This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Don't blame Java for this
Posted by Kishori sharan on July 02, 2001 at 10:08 AM
Floating-point calculation is rarely exact. This is not a problem with Java float data type. Rather, this is the problem with the floating-point storage format. Most real numbers cannot be exactly represented in binary form and so while storing these numbers in memory computer has to round the numbers. This includes a rounding errors in almost every floating-point numbers stored in computers and you cannot do anything about it. By putting some extra effort you can just optimize the floating-point calculations which will inlcude little less roundng errors. In my opinion, when you are converting the string in flosts taking only two digits after decimal, take more decimal digits and perform the calculations. Finally, just take onlt two decimal digts from your result. This may give you correct result to some extent. However, there is no guarantee. But, don't blame Java for this.