If you know that your numbers are multiples of e.g. 0.01, I would suggest that you convert to double, round to the nearest integer, and subtract that to get the fractional residue. Multiply that by 100, round to the nearest integer, and then divide by 100. Add that to the whole-number part to get the nearest double
representation to the multiple of 0.01 which is nearest the original number.
Note that depending upon where the float
values originally came from, such treatment may or may not improve accuracy. The closest float
value to 9000.02 is about 9000.019531, and the closest float
value to 9000.021 is about 9000.021484f. If the values were arrived at by converting 9000.020 and 9000.021 to float
, the difference between them should be about 0.01. If, however, they were arrived at by e.g. computing 9000f+0.019531f
and 9000f+0.021484f
, then the difference between them should be closer to 0.02. Rounding to the nearest 0.01 before the subtract would improve accuracy in the former case and degrade it in the latter.
Single
toDouble
while maintaining representability in case the value was indeed hard-coded, as the values are serialized at some point. I think I understand that directSingle
toDouble
conversion is not harmful, but this is an issue of presentation.