Rounding problems are common when display floating point numbers, as most floating point numbers do not have a precise representation in binary. So even if you were to use float
s instead of doubles
, the problem of small approximation errors would still arise.
You have two choices. You can work with integers only, for example by scaling (multiplying) all of your numbers by 100 or 1000, and then remembering to scale them back when you produce output for your users. Or, you can treat the display problem as a separate issue, and when you come to render numbers in your output, use a number formatting library, such as the one built in to Java, or other suggestions on StackOverflow. Personally, I would make the second choice.