How to solve the problem caused by the toFixed () method in JS _ javascript skills

Source: Internet
Author: User
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 in five into double". Recently, we found that the toFixed () method in JS has some problems, using the toFixed () on the prototype object of the native Number object () the rules are not "rounding up" or "four homes, six homes, six 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:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.