[solved] Why is 65.4 times 100in js equal to 6540.000000000001

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

Mar.14,2021

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

Menu