Possible Duplicate:
Java floats and doubles, how to avoid that 0.0 + 0.1 + … + 0.1 == 0.9000001?
I am having a following problem in Java - I need to iterate between 0.1f and 1.0f in 0.1f increments,so I would like my output to look like this:
0.1
0.2
0.3
0.4
...
0.9
Instead,when I do:
for(float i = 0.1f; i < 1f; i += 0.1f)
System.out.println(i);
I get
0.1
0.2
0.3
0.4
0.5
0.6
0.70000005
0.8000001
0.9000001
I imagine it has something to do with the way fractions are represented by a computer,but I would like to know why is this,and if there is anything I can do to stop it. thanks.