So, the question is, would you javascript (js) decimal multiplication division problem?

Calculated using js

12.32 * 7 The result is how much?


Answer: 86.24000000000001


Why is there such a problem? How to deal with it?


js there is a bug in dealing with decimal multiplication and division, the solution can be: a decimal integer into a deal.


The above calculation can be changed to:

12.32 * 100 * 7 /100

The result is: 86.24, correct.


Also re-calculate:

8.80 * 100 * 12 / 100

Results: 105.60000000000002

38.80 A similar problem can also occur.


10-fold increased accuracy:

8.80 * 1000 * 12 / 1000

Result: 105.6

Normal.


16.40 * 1000000 * 6 / 1000000

The results are also problems


In order to perform more accurate js directly expand in the future value of decimal calculations js 10,000 times, and then divided by 10000, can solve the problem.

There num = 38.80;

was num2 = 13;

alert(num * 10000 * 12 / 10000);


The multiplication and division of this number 10000 has been tested the most appropriate, some small numbers problem, big (1000000) Some figures are wrong. 

Welcome to work one to five years of Java Java technology engineer friends to join exchange group: 659 270 626
provides free learning materials within the Java architecture group (which has high availability, high concurrency, high performance and distributed, Jvm performance tuning, Spring Source, MyBatis, Netty, Redis, Kafka, Mysql, Zookeeper, Tomcat, Docker, Dubbo, Nginx and other knowledge of information architecture) rational use of every minute of their own time to enhance their learning, do not use the "no time "to hide his ideological laziness! Young, hard fight, give an account of their own future!


Guess you like

Origin blog.51cto.com/14028890/2413070