This content has been marked as final. Show 1 reply
I am using valueOf("168981.29").floatValue()) to convert a string into floatWhy? Why not just use the literal number?
The issue is with some numbers which is ending with .29 .The issue is that floats only have a precise representation of decimal fractions if the fraction is a multiple of a negative power of 2, e.g. .5, .25, .75, .125, .375, .625, .875, ...
If you have problems with the rounding you are getting you probably shouldn't be using floating-point at all, you should be using a decimal format.
This is not a Java question so much as a basic computer-science floating-point question. Search for a paper entitled 'What every computer scientist should know about floating-point'.