This discussion is archived
7 Replies Latest reply: Apr 11, 2009 10:36 PM by 800308 RSS

Having problems with the decimal  format

843789 Newbie
Currently Being Moderated
I am have problems getting my program to show the amount in the decimal format. I am new to these and have been correcting and try to get it all day. Can someone help me?

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

/* Diana Saddler
 * PRG-421  Java Programming II
 */

public class mortgage extends JFrame implements ActionListener {
private JPanel panelAdder;
private JLabel labela;
private JLabel labelt;
private JLabel labelr;
private JTextField textFieldAmount;
private JTextField textFieldTerm;
private JTextField textFieldRate;
private JTextField textFieldResult;
private JButton buttonCalc;

public mortgage() {
  initComponents();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setVisible(true);
  pack();

  // Add Listeners
  buttonCalc.addActionListener(this);
}

public void initComponents() {

 //Initialize Components
 panelAdder = new JPanel();
 labela = new JLabel("Amount");
 textFieldAmount = new JTextField();
 labelt = new JLabel("Term");
 textFieldTerm = new JTextField();
 labelr = new JLabel("Rate");
 textFieldRate = new JTextField();
 textFieldResult = new JTextField();
 buttonCalc = new JButton("Calculate");

 //Set Object Attributes
 textFieldResult.setEditable(false);
 textFieldResult.setColumns(8);
 textFieldAmount.setColumns(6);
 textFieldTerm.setColumns(3);
 textFieldRate.setColumns(3);

 Container contentPane = getContentPane();
 contentPane.setLayout(new FlowLayout());

 //Add the components to the panel
 panelAdder.add(labela);
 panelAdder.add(textFieldAmount);
 panelAdder.add(labelt);
 panelAdder.add(textFieldTerm);
 panelAdder.add(labelr);
 panelAdder.add(textFieldRate);
 panelAdder.add(buttonCalc);
 panelAdder.add(textFieldResult);

 contentPane.add(panelAdder);
}


    public static void main(String[] args) {
         mortgage frame = new mortgage();
         DecimalFormat money = new DecimalFormat("0.00");

}

 //calculate
    private void setResultValue() {
   double amount = Double.parseDouble(textFieldAmount.getText());
   double term = Integer.parseInt(textFieldTerm.getText());
   double rate = Double.parseDouble(textFieldRate.getText()) / 100.;
   double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );

   textFieldResult.setText(Double.toString(result));
   java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");

}
public void actionPerformed(ActionEvent event) {
  System.out.println("Action Button");
  String command = event.getActionCommand();
  if ("Calculate".equals(command))
      setResultValue();
}
}
  • 1. Re: Having problems with the decimal  format
    843789 Newbie
    Currently Being Moderated
    what is the problem exactly?

    Edited by: scphan on Apr 11, 2009 10:39 PM
  • 2. Re: Having problems with the decimal  format
    843789 Newbie
    Currently Being Moderated
    ignore this post

    Edited by: scphan on Apr 11, 2009 10:59 PM
  • 3. Re: Having problems with the decimal  format
    800308 Newbie
    Currently Being Moderated
    Quick2u,
      //calculate
      private void setResultValue() {
        double amount = Double.parseDouble(textFieldAmount.getText());
        double term = Integer.parseInt(textFieldTerm.getText());
        double rate = Double.parseDouble(textFieldRate.getText()) / 100.;
        double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
        textFieldResult.setText(Double.toString(result));
        java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");
      }
    What did you suppose dec would do? Magically format the number in the text field? You need to use dec... And how do we do that? Why we call methods on the class... now let's see which method should we call I wonder... so we consult [the API|http://java.sun.com/javase/6/docs/api/java/text/DecimalFormat.html] where we find a list of all the methods in the DecimalFormat with a succinct explanation of what each one does... And we scan that list of method names and which one do you suppose might format a number? Hmmm.

    You are far to <censored/> to be a programmer. Take up golf.

    If you still need help then google "java NumberFormat example"

    Cheers. Keith.
  • 4. Re: Having problems with the decimal  format
    843789 Newbie
    Currently Being Moderated
    No, for your info. I did not say that I'm a programmer. I am a new student in java programming and needed some help. So who the dumb <censored/> now! If you were not here to help then don't post here.

    Cheers that, Keith
    Try Google already.
  • 5. Re: Having problems with the decimal  format
    843789 Newbie
    Currently Being Moderated
    it won't help you to be rude to corlettk, he was offering you genuine advice and that is to check and read up on the java api (what almost all java programmers do)for the DecimalFormat class so you'll be able to customize your program accordingly

    btw, just use a StringBuffer ( instantiate and assign to a variable which later on will be sent as an argument to setText() ), and use a new FieldPosition with an argument to the constructor being NumberFormat.INTEGER_FIELD

    this is for the method format() of DecimalFormat which will format your number value (read api) and append it to your StringBuffer

    i've never used DecimalFormat before until now and made it work just by taking a close look at the api ("words format (notice it's the almost the only one that returns something hence a stringbuffer and accepts a double for an argument), the parameters it needed (tried instantiating with a stringbuffer but leaving the other two arguments null didn't work so i gave it non-null arguments which led me to figuring out how to instantiate a fieldposition object)

    it'll probably help you more to read the api before you consult us on specific problems
  • 6. Re: Having problems with the decimal  format
    843789 Newbie
    Currently Being Moderated
    Not trying to be rude to you, but if you look at corlettk post, he was rude. And I do not like when people curse at me for trying to ask a question. As I stated before I AM NOT A PROGRAMMER. I AM A BEGINNING STUDENT IN JAVA PROGRAMMING. I look in my textbookk for help and looking on the internet does not help because I guess I don't understand. I am new to this. Thank you.
  • 7. Re: Having problems with the decimal  format
    800308 Newbie
    Currently Being Moderated
    scphan wrote:
    it won't help you to be rude to corlettk, he was offering you genuine advice and that is to check and read up on the java api (what almost all java programmers do)for the DecimalFormat class so you'll be able to customize your program accordingly

    btw, just use a StringBuffer ( instantiate and assign to a variable which later on will be sent as an argument to setText() ), and use a new FieldPosition with an argument to the constructor being NumberFormat.INTEGER_FIELD

    this is for the method format() of DecimalFormat which will format your number value (read api) and append it to your StringBuffer

    i've never used DecimalFormat before until now and made it work just by taking a close look at the api ("words format (notice it's the almost the only one that returns something hence a stringbuffer and accepts a double for an argument), the parameters it needed (tried instantiating with a stringbuffer but leaving the other two arguments null didn't work so i gave it non-null arguments which led me to figuring out how to instantiate a fieldposition object)

    it'll probably help you more to read the api before you consult us on specific problems
    I her defence... I was rude first... But yeah... did you read the rest? and try to apply it? How'd ya' go?

    One of the first things you need to learn about programming is that nobody knows everything... the trick is to learn how to look for relevent information... and filter the nuggets from the dross... You went straight for the dross. Epic fail.

    Cheers. Keith.