varA =0.3-0.2; varb = 0.4-0.3; Alert (A+"&"+b); if(A = =b) {alert ("true"); } Else{alert ("false"); }
0.09999999999999998&0.10000000000000003
The result is false
Avascript has only one numeric type number, and all the numbers in JavaScript arerepresented in the ieee-754 standard format. The accuracy of floating-point numbers is not specific to JavaScript, because some decimal places are infinite in binary notation: When calculating decimal in Java and JavaScript, decimal decimals are first converted to the corresponding binary, and some decimals are not fully converted to binary. This is 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. To avoid this, you can usually enlarge the integer multiples of the same 10 at the same time, and after the calculation is complete, the integer multiples added before being removed.
decimal binary binary0.1 0.0001 1001 1001 1001 ...0.2 0.0011 0011 0011 0011 ...0.3 0.0100 1100 1100 1100 ...0.4 0.0110 0110 0110 0110 ...0.5 0.10.6 0.1001 1001 1001 1001...
JS small number of difference, compare size