JS processing data rounding, tofixed and round difference

Source: Internet
Author: User
Tags rounds

1. tofixed Method

  The ToFixed () method rounds numbers to a number that specifies the number of decimal digits. For example, if the data Num is reserved for 2 decimal places, it is represented as:toFixed (Num), but its rounding rules differ from those in mathematics, using banker rounding rules, bankers rounding: the so-called banker rounding method, Its essence is a four-house six into the five-take (also known as the four-six into the five left double) method. the specific rules are as follows:

In short, it is: four six into five considerations, five after non-zero into one, five after the zero look at parity, five ago for I should shed, five before for odd to go into one.

It is clear that this rule does not conform to the way we normally deal with data. To solve this problem, you can customize the implementation using the Math.Round method to specify how many bits of data are reserved for processing.

2. Round method

The round () method rounds a number to the nearest integer. For example:math.round (x) is the integer that takes x nearest to it. The method of trade-offs uses the method of rounding, which conforms to the rules of choice in mathematics. The processing of decimals is not so convenient, but can be customized according to different requirements.

For example, for X to preserve two-bit decimals, you can use Math.Round (x * 100)/100 for processing.

 

  

JS processing data rounding, tofixed and round difference

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.