In a standard browser, the value obtained using this method looks a lot like rounding.

But in fact, it is not rounding in the general mathematical sense. We can find counterexamples as follows:

img

In the first three examples above, the third decimal place is all 5. Normally, it should be added to 1, but here are all rounded off. The fourth is also 5, but the result is that 5 is rounded off and the previous digit is added to 1. . Does it feel a bit confusing ( ̄ェ ̄;)?

Many people on the Internet say that this method is banker rounding , the so-called banker rounding method, its essence is a rounding six to five taking even method (also known as rounding six to five leaving double).

The specific rules are: rounding to five is considered, after five is non-zero, then one is added, after five is zero to see odd-even, five before five is even should be rounded, five before five is odd and one is added.

According to this rule,

The value obtained by 10.1250.toFixed(2) 10.12 (even before five should be discarded)

The value obtained by 10.3150.toFixed(2) 10.32 (Five years ago is an odd one)

But in fact, try it and the result is as follows

img

So, it is not banker rounding either.

I checked its calculation rules, and it looks like this:

<u>If the next number of the specified decimal place is not 5, it will be rounded to six. If it is 5, first get a value A that is rounded to 5 and retain the previous number, and a value B that is rounded to 5 and the previous number is rounded to 1. Use the two values to subtract the original number to compare the two differences The absolute value of, take A or B corresponding to the difference of the smaller absolute value as the return value. If the absolute value of the difference is the same, it returns the value of the previous number after rounding up to 1, which is to return to B. </u>It may not be easy to understand, the case is as follows:

10.215.toFixed(2) , two decimal places are specified, and the number after the specified number is 5, and the above rules are executed

First get two values A=10.21 B=10.22

A-10.215=-0.004999999999999005

B-10.215=0.005000000000000782

The absolute value of the first difference is small, so the final return is A 10.21

10.515.toFixed(2) A=10.51 B=10.52

A-10.515=-0.005000000000000782

B-10.515=0.004999999999999005

The absolute value of the second difference is small, so the final return is B 10.52

10.125.toFixed(2) A=10.12 B=10.13

A-10.125=-0.005000000000000782

B-10.125=0.005000000000000782

The absolute value of the two differences is equal, so the final return is B 10.13

The above is the calculation rule of toFixed method in standard browser.

If it is in the IE browser, the toFixed method has become a real rounding (only simulated in IE11 for other versions of IE, please refer to the actual performance).

img

Summary: uses js native toFixed() for rounding, you are likely to get a wrong result.

Other rounding methods available:

Math.round(xx.xxx*100)/100


MangoGoing
785 声望1.2k 粉丝

开源项目:详见个人详情