I have a
map<Float,Object>.
I need it store valuse like
0.01 ->ObjA
0.02 ->ObjB
and sometimes
0.001 ->ObjA
0.002 ->ObjB (three digits after the dot)
I thought it would be easy.
But I have notice that when I calculate simple calculation with the float
I sometimes get stuff like this.
0.09998
0.02001 —– instead of (0.01, 0.02)
I know that float isnt that good for that usage so I have tried double and clearly I got a map filled with
0.0999999998
0.0200000001
naturally.
So I think I should round it. but it is not that easy since I dont know to which size.
sometimes it should be 0.03 and sometimes 0.3.
Thanks for your assistance.
EDIT: Thanks for all the answers. I think my problem is that I get 5.00001 instead of 5 . not that I enter 5.000001 to a map. I would like to round the float and each time to the best value I can find . and since it is alwas 0.0000000X or 0.999999X I think It should be easy. But I haven’t figured it out yet.
Try using
BigDecimal will allow you to define floating point values exactly.