Possible Duplicate:
Why can't decimal numbers be represented exactly in binary?
i get very odd behaviour from simple float maths. eg.
//create a float with value of 1.0
float f = 1.0;
//take 0.1 from its value
f -=0.1;
for the first few times when i minus 0.1 it returns 0.9, 0.8, 0.7……
then for some reason it will return 0.699999999999, 0.59999999999 and so on.
to say this is unexpected is an understatement.
so to fix this i either need to know why it would do this
or a math function similar to Round(float) where it will round the number from 0.5999999 to 0.6.
thank you
edit,
ok sorry for asking lol
any fix available? like Round(float) kinda thing?
other edit:
for the next person to ask about this heres a fix
final DecimalFormat myFormat = new DecimalFormat("#.#");
myFormat.format(myFloatValue)
this will change myFloatValue 0.599999 into 0.6
A computer is a finite device, so it stores floating point numbers with a finite precision. And it stores them as binary floating point numbers — that is relative to base 2 instead of base 10. A number with a finite representation as a decimal fraction doesn’t necessarily have a finite representation as a binary number, so it must be rounded to be stored in a finite computer. In this example,
0.1will be rounded towhen stored as a double precision floating point number, so you actually subtract a bit more than
0.1in each step.