Analysis
JavaScript has only one number type, Number , and all numbers in Javascript are represented in the IEEE-754 standard format. The precision problem of floating point numbers is not specific to JavaScript, because some decimals have infinite digits when represented in binary:
Decimal System 0.0100 1100 1100 1100 ...
0.4 0.0110 0110 0110 0110 ...
0.5 0.1
0.6 0.1001 1001 1001 1001 ...
So for example, 1.1, the program cannot actually represent ‘1.1’, but can only achieve a certain degree of accuracy. This is an inevitable loss of precision:
The problem is more complicated in JavaScript. Here are just some test data in Chrome:
Input
1.0-0.9 == 0.1 False
1.0-0.8 == 0.2 False1.0-0.7 == 0.3 False
1.0-0.6 == 0.4 True
1.0- 0.5 == 0.5 True
1.0-0.4 == 0.6 True
1.0-0.3 == 0.7 True
1.0-0.2 == 0.8 True
1.0-0.1 == 0.9 True
Solution
Number.prototype.isEqual = function(number, digits){
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