Solved

# how to subtract decimal????

Posted on 2002-05-27
351 Views
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");
{
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == subtract)
{
double a = new Double(tf1.getText()).doubleValue();
double b = new Double(tf2.getText()).doubleValue();

System.out.println(a-b);
}
}

});

}//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???

0
Question by:rdy1437
• 7
• 4
• 3
• +1

LVL 92

Expert Comment

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.
0

Author Comment

can you provide sample code for this??
0

LVL 92

Expert Comment

BigDecimal a = new BigDecimal("50.56");
BigDecimal b = new BigDecimal("25");
BigDecimal c = a.subtract(b);
System.out.println(c);

0

LVL 92

Expert Comment

Floating point numbers are only an approximation (albeit quite an accurate one).
0

LVL 35

Expert Comment

There is also an excellent tutorial on BigDecimal: http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
0

LVL 2

Expert Comment

// 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");
{
public void actionPerformed(ActionEvent e)
{
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
if (e.getSource() == subtract)
{
double a = new Double(tf1.getText()).doubleValue();
double b = new Double(tf2.getText()).doubleValue();
double result =(double) a - (double) b;
System.out.println( twoDigits.format(result) );
}
}
});
}//End of main
}
0

LVL 2

Expert Comment

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
0

LVL 92

Expert Comment

vogen,

1. Please avoid proposing answers as it locks the question and make it harder for other experts to contribute.

0

Author Comment

0

LVL 92

Expert Comment

Good to hear it helped :-)
0

LVL 2

Expert Comment

solly, i was only trying to help.. ::(
0

Author Comment

hey objects how can i give you the points?
0

LVL 92

Accepted Solution

objects earned 50 total points
reject the proposed answer, and accept my comment as an answer :)
0

LVL 92

Expert Comment

0

LVL 2

Expert Comment

I spent my time and this is what I get
it's not fair
thanks anyway
0

## Featured Post

### Suggested Solutions

hash value 2 38
Running Jira on Raspberry PI 2? 3 117
base64 decode encode 12 94
compre toata in where clue oracle 4 41
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.