When Javascript performs the product operation of floating point numbers, multiple decimal places may occur.
This is because the floating-point numbers are first converted into binary before calculation. However, some decimals appear in infinite loops after binary encoding, which leads to errors in calculations. This is also the case in other programming languages. Similar questions.
The explanation of the reason is known from Baidu:
For example: find 1038.1-1000
1038.1=10000001110.0001100110011001100110011001100110011001100....
1 000 =1111101000
1038.1 conversion Binary is an infinitely recurring decimal, and 1100 is a cyclic section. Only approximate values can be taken. The error is caused here. If the browser version is high, you can use the toFixed() method to round the Number to a number with specified decimal places.
Solution: According to the number of decimal places to be retained (such as 4), first multiply by (10^4) when calculating the product, then divide the calculation result by (10^4), and finally divide the result Take the approximate value Math.round