I store some data in a database and I read those data with a cursor. All the data are 56.45 , 3.04, 0.03 type, so two numbers after the decimal point. Now I would like to sum th
Your numbers are represented in decimal to you, but binary to a computer.
In both decimal and binary, some fractions cannot be represented precisely.
For example 1/3 cannot be represented precisely in decimal. You can merely approximate it with something like 0.3333333333. But then if you try to do 3 * 0.3333333333, you end up with 0.9999999999 instead of 1.0000000000.
In binary, you have the same problem, but with different fractions.
So, sometimes when you convert from decimal to binary and back again (which is what the computer basically must do to do floating point arithmetic for you), you get small errors in precision even though all the numbers are perfectly precise in decimal. When the computer uses their binary near-equivalents, approximations are introduced and they result in small errors.
This is actually a very common issue in programming. For a lot more detail, see What Every Computer Scientist Should Know About Floating-Point Arithmetic.
Other approach, you can define your sum variable as double
and then you can set NumberFormat
for your sum variable.
Example:
double sum = 21.2015;
NumberFormat formatter = new DecimalFormat("#.##");
formatter.format(sum);