as in question ~
how to completely eradicate the problem of floating-point operation accuracy?
rounding is not reasonable in terms of money
in the way of * 100s / 100s, precision problems still occur when special values are encountered
as in question ~
how to completely eradicate the problem of floating-point operation accuracy?
rounding is not reasonable in terms of money
in the way of * 100s / 100s, precision problems still occur when special values are encountered
do you remember 0.1 / 0.2
? Because JS uses the IEEE 754 double-precision version (64-bit), and any language that uses IEEE 754 has this problem. The computer indicates that the decimal system is expressed in binary, the integer is divided by 2, the quotient continues to be divided by 2, and the remainder is arranged in reverse order; the decimal is multiplied by 2, the decimal part continues to be multiplied by 2, the decimal part is rounded, and the decimal part 0 is obtained. arrange the integers in order.
for example, the binary of 22 is 10110, the binary of 0.8125 is 0.1101. So, the binary of 0.4: 0.4 = 0.01100110 (0110)
( (0110) represents a loop ), which is the binary of an infinite loop.
solution: you can use the toFixed () method, which rounds Number to the specified number of decimal places: parseFloat ((65.4x100) .tofixed (10))
the landlord can take a look at this link: several advanced front-end question parsing
I hope I can help you.
it is more convenient for money to be divided into units
Previous: Why is css not on one line in Chinese and semantic on one line in English?
Next: Wechat web page long press the QR code picture can pop up menu is how to achieve?