Android number format is wrong somehow, instead of 3.5 I get 3.499999999, why?

為{幸葍}努か 提交于 2019-12-02 01:23:29

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);

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!