First:
What's the situation?
Reason: JS uses binary for fractional calculation
First look at the decimal conversion to binary binary method:
The integer digits of the decimal number are the integer digits of the binary number, and the decimal digit is the scale of the binary number.
If we have a decimal 111.4 (10), we'll calculate it for him:
First take the decimal digit 0.4, the "multiply two take the whole method"
0.4*2=0.8 the number of the integer position of the result is 0 | By
0.8*2=1.6 Take 1 | Shun
0.6*2=1.2 Take 1 | Order
0.2*2=0.4 Take 0 | Write
0.4*2=0.8 Take 0 | Out
0.8*2=1.6 Take 1 |
0.6*2=1.2 Take 1 |
0.2*2=0.4 Take 0 |
Come here, we find that there is a loop, so we can infer that the binary after the decimal point is
0.01100110 ... (Cycle 0110)
The whole number is not written, except for the 2 method of taking the remainder
The problem of 2.6+2.6+2.6=7.800000000001,javascript in the calculation of decimal operations, the decimal number will be converted to the corresponding binary, part of the decimal can not be fully converted to binary, where the first error occurs. After the decimal is converted to binary, the binary operation is performed, and the binary result is obtained. The binary results are then converted to decimal, where the second error is usually present.
Workaround: Specify the number of decimals (7.8) to keep. ToFixed (2) = 7.80; This method has the potential to lose precision, and there is a way to customize this addition on the Web, plus a link
A solution to the problem of multiple decimal places in JS decimal operation
js-Decimal Calculation problem