PDA

View Full Version : precision question


centenial
09-28-2009, 04:19 AM
I'm confused as to the precision of this output:


double change;

change = 45.69;
change -= 40;

System.out.println(change);


This code outputs 5.689999999999998. I would expect it to output 5.69.

Could someone explain to me why this is happening, and what I need to do for it to retain precision of the correct number?

Thanks!

mmcnitt
09-28-2009, 05:46 AM
not sure, but if all you want is 2 decimal places i would say try using float instead of double

Apothem
10-01-2009, 03:53 AM
I tried that code snippet and it did print out 5.689999999999998. Maybe some code you had in between there caused the problem?

mamo
10-01-2009, 10:01 PM
(try this one)
final double change;
change = 45.69f;
(without the f , the compiler will generate an error )