The precision of floating point numbers is not unique to JavaScript, because some decimals represent infinite digits in binary format.
Analysis JavaScript has only one numeric type Number, and all numbers in Javascript are represented in IEEE-754 standard format. The precision of floating point numbers is not unique in JavaScript, because some decimal places indicate that the number of digits is infinite: decimal binary 0.1 0.0001 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... therefore, for example, 1.1, the program cannot really represent '1. 1 ', but can only achieve a certain degree of accuracy, this is an unavoidable loss of precision: 1.09999999999999999 in JavaScript problems are more complex, here only some test data in Chrome: input and Output 1.0-0.9 = 0.1 False1.0-0.8 = 0.2 False1.0-0.7 = 0.3 False1.0-0.6 = 0.4 True1.0-0.5 = 0.5 True1.0-0.4 = 0.6 True1.0-0.3 = 0.7 True1.0-0.2 = 0.8 True1.0-0.1 = 0.9 True how to avoid such 1.0-0.9! = What happens to 0.1 of non-bug issues? The following provides a more widely used solution. The accuracy of the computing result is reduced before the floating point calculation result is judged, because the accuracy is automatically rounded down: the Code is as follows: (1.0-0.9 ). toFixed (digits) // toFixed () The precision parameter must be between 0 and 20 parseFloat (1.0-0.9 ). toFixed (10) = 0.1 // The result is TrueparseFloat (1.0-0.8 ). toFixed (10) = 0.2 // The result is TrueparseFloat (1.0-0.7 ). toFixed (10) = 0.3 // The result is TrueparseFloat (11.0-11.8 ). toFixed (10) ===- 0.8 // The result is True. The extracted code is as follows: // use the isEqual tool to determine whether the values are equal. function isEqual (number1, number2, Digits) {digits = undefined? 10: digits; // The default precision is 10 return number1.toFixed (digits) === number2.toFixed (digits);} isEqual (1.0-0.7, 0.3 ); // return true // native extension mode, prefer the object-oriented style Number. prototype. isEqual = function (number, digits) {digits = undefined? 10: digits; // The default precision is 10 return this. toFixed (digits) === number. toFixed (digits);} (1.0-0.7 ). isEqual (0.3); // return true