Recently, we found that the toFixed () method in JS has some problems. When we use the toFixed () method on the prototype object of the native Number object, the rule is not a so-called "Rounding" or "four homes, six homes, five homes, and five into double". The so-called "four homes, six homes, and five into double" is explained in Baidu Encyclopedia: that is to say, "4 homes, 6 homes, 5 shopping cart," 4 "here refers to rounding up when 4 or less, and" 6 "refers to turning around when 6 or more, "5" refers to the number after 5. When there are numbers after 5, 5 is included into 1; when there is no valid number after 5, there are two cases: ① 5 is an odd number, and 5 is included in 1; ② 5 is an even number, and 5 is absent. (0 is the smallest even number ). Several examples on Baidu encyclopedia are true in actual situations, but they are not scientific and cannot cover all situations.
Test Browser: the browser IE6 and the advanced browser IE78 and all mainstream modern browsers include IE9, IE10, FF, chrome, opera, and safari. (Note: When you use a development tool similar to firebug in IE10, the test results are inconsistent with those of the native earlier version of IE browser when you use the mode compatible with the IE earlier version)
There is no dispute over rounding at the end of a floating point value equal to or greater than 4 or greater than 6, but it is confusing when the end is equal to or equal to 5.
Summary: As we all know, precision loss may occur in floating point computing that follows the IEEE754 numeric format, and ES is not the only one. Therefore, do not test a specific floating point value, for example: 0.1 + 0.2;
Solution: override the Number. prototype. toFixed () method: