rdy1437
asked on
how to subtract decimal????
hi guys!!!!
i had a program that subtract two double variable.here's my code:
import java.awt.*;
import java.awt.event.*;
public class Try
{
public static void main(String argv[])
{
Frame fr = new Frame();
fr.pack();
fr.setVisible(true);
Panel panel = new Panel();
final TextField tf1 = new TextField(15);
final TextField tf2 = new TextField(15);
final Button subtract = new Button("Minus");
Button clear = new Button("Clear");
subtract.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEven t e)
{
if (e.getSource() == subtract)
{
double a = new Double(tf1.getText()).doub leValue();
double b = new Double(tf2.getText()).doub leValue();
System.out.println(a-b);
}
}
});
panel.add(tf1);
panel.add(tf2);
panel.add(subtract);
panel.add(clear);
fr.add(panel);
}//End of main
}
OUTPUT:
TF1 = 50.56
TF2 = 25
DIFFERENCE = 25.560000000000002
how would i make it so that it will only return 25.26???
thanks in advance!!!
i had a program that subtract two double variable.here's my code:
import java.awt.*;
import java.awt.event.*;
public class Try
{
public static void main(String argv[])
{
Frame fr = new Frame();
fr.pack();
fr.setVisible(true);
Panel panel = new Panel();
final TextField tf1 = new TextField(15);
final TextField tf2 = new TextField(15);
final Button subtract = new Button("Minus");
Button clear = new Button("Clear");
subtract.addActionListener
{
public void actionPerformed(ActionEven
{
if (e.getSource() == subtract)
{
double a = new Double(tf1.getText()).doub
double b = new Double(tf2.getText()).doub
System.out.println(a-b);
}
}
});
panel.add(tf1);
panel.add(tf2);
panel.add(subtract);
panel.add(clear);
fr.add(panel);
}//End of main
}
OUTPUT:
TF1 = 50.56
TF2 = 25
DIFFERENCE = 25.560000000000002
how would i make it so that it will only return 25.26???
thanks in advance!!!
That's simply a rounding error resulting from floating point arithmetic. If you need to avoid this then use the BigDecimal class, or simply format your output to two decimal places.
ASKER
can you provide sample code for this??
BigDecimal a = new BigDecimal("50.56");
BigDecimal b = new BigDecimal("25");
BigDecimal c = a.subtract(b);
System.out.println(c);
BigDecimal b = new BigDecimal("25");
BigDecimal c = a.subtract(b);
System.out.println(c);
Floating point numbers are only an approximation (albeit quite an accurate one).
There is also an excellent tutorial on BigDecimal: http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
// rdy1437, here is the solution:
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Try
{
public static void main(String argv[])
{
Frame fr = new Frame();
fr.pack();
fr.setVisible(true);
Panel panel = new Panel();
final TextField tf1 = new TextField(15);
final TextField tf2 = new TextField(15);
final Button subtract = new Button("Minus");
Button clear = new Button("Clear");
subtract.addActionListener (new ActionListener()
{
public void actionPerformed(ActionEven t e)
{
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
if (e.getSource() == subtract)
{
double a = new Double(tf1.getText()).doub leValue();
double b = new Double(tf2.getText()).doub leValue();
double result =(double) a - (double) b;
System.out.println( twoDigits.format(result) );
}
}
});
panel.add(tf1);
panel.add(tf2);
panel.add(subtract);
panel.add(clear);
fr.add(panel);
}//End of main
}
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class Try
{
public static void main(String argv[])
{
Frame fr = new Frame();
fr.pack();
fr.setVisible(true);
Panel panel = new Panel();
final TextField tf1 = new TextField(15);
final TextField tf2 = new TextField(15);
final Button subtract = new Button("Minus");
Button clear = new Button("Clear");
subtract.addActionListener
{
public void actionPerformed(ActionEven
{
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
if (e.getSource() == subtract)
{
double a = new Double(tf1.getText()).doub
double b = new Double(tf2.getText()).doub
double result =(double) a - (double) b;
System.out.println( twoDigits.format(result) );
}
}
});
panel.add(tf1);
panel.add(tf2);
panel.add(subtract);
panel.add(clear);
fr.add(panel);
}//End of main
}
DecimalFormat object that is initialized with the string "0.00". Each 0 is a format flag that specifies a required digit position in the formatted floating-point number.
pls ask if have any question..
cheers
pls ask if have any question..
cheers
vogen,
1. Please avoid proposing answers as it locks the question and make it harder for other experts to contribute.
2. I had already suggested the option of using formatting.
1. Please avoid proposing answers as it locks the question and make it harder for other experts to contribute.
2. I had already suggested the option of using formatting.
ASKER
thanks guys for your comments.i already used the comment made by objects...
Good to hear it helped :-)
Can you accept my comment as an answer then please.
Can you accept my comment as an answer then please.
solly, i was only trying to help.. ::(
ASKER
hey objects how can i give you the points?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I spent my time and this is what I get
it's not fair
thanks anyway
it's not fair
thanks anyway