This article mainly introduces the division of decimal points in javascript (js) in detail. If you need a friend, please refer to it and hope to help you.
I. js computing
What are the results of 12.32*7? Answer: 86.24000000000001
Why is this problem? How can this problem be solved?
Js has a bug in processing the multiplication and division of decimals. The solution can be to convert decimals into integers.
The above calculation can be changed:
12.32x100x7/100
The result is: 86.24, correct.
In addition, calculate the following:
8.80x100x12/100
Result: 105.60000000000002
38.80 will also have similar problems.
Precision increased by 10 times:
8.80x1000x12/1000
Result: 105.6
Normal.
16.40x1000000x6/1000000
The result is also incorrect.
In order to make javascript execution more accurate, the value will be directly increased by 10000 times in subsequent JavaScript decimal calculations, and then divided by 10000 to solve the problem.
Var num = 38.80;
Var num2 = 13;
Alert (num x 10000*12/10000 );
The number to be multiplied and divided is the most suitable for tests of 10000. If the number is smaller, there is a problem. If the number is larger (1000000), there are also problems.
II,
The Code is as follows: