JS processing data rounding (the difference between tofixed and round), tofixedround

Source: Internet
Author: User
Tags rounds

JS processing data rounding (the difference between tofixed and round), tofixedround

1. tofixed Method

The toFixed () method rounds a Number to a specified Number of decimal places. For example, if the data Num is retained with two decimal places, it is represented as: toFixed (Num). However, the Rounding Rule is different from that in mathematics, and the banker Rounding Rule is used,Bankers Rounding: the so-called bankers rounding method is essentially a four-house-six-in-five pair (also known as four-house-six-in-five-stay pair) method.The specific rules are as follows:

Simply put, four homes, six homes, five homes, five homes, non-zero homes, five homes, zero homes, parity, five homes, five homes, and five homes are odd homes.

Obviously, this rule does not conform to the common data processing method. To solve this problem, you can use the Math. round method to customize the implementation of specifying the number of data records to be retained for processing.

2. round Method

The round () method rounds a number to the nearest integer. For example, Math. round (x) takes x as its nearest integer. The trade-off method is a rounding method that complies with the trade-off rules in mathematics. Decimal processing is not that convenient, but can be customized according to different requirements.

For example:Math. round (X * 100)/100. Can be used to retain two decimal places of X.

The above JS processing data is rounded down (the difference between tofixed and round), which is all the content that I have shared with you. I hope to give you a reference and support for the customer's house.

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.