x
Solved

# double numbers

Posted on 2004-10-05
Medium Priority
235 Views
Hi dear experts;

I have two double numbers like following
double A= 0.6;
double B=0.5;

when i try to subtract B from A like following
system.Out.print(A-B);
it gives me  0.0999999998  but i want the result to be  0.10 or 0.1
how can i do this ?  i try ceil floor and round methods of Math class but they didint work
it is an error of java ?
because  when i try to 0.8-06= 0.200000000000004  i want to see result like 0.2
Sincerely,

0
Question by:elmakarge
• 2

LVL 18

Accepted Solution

armoghan earned 750 total points
ID: 12225282
see this for rounding the double
0

LVL 18

Expert Comment

ID: 12225310
public static final double roundDouble(double d, int places)
{
return Math.round(d * Math.pow(10, (double) places)) / Math.pow(10,            (double) places);
}
0

LVL 11

Expert Comment

ID: 12225325
This is because of the nature of floating point numbers, and arithmetic... the .999999998 result is not an error....
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.