The difference between using tofixed and round in JS to process data rounding is tofixedround.

Source: Internet
Author: User
Tags rounds

The difference between using tofixed and round in JS to process data rounding is 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. It uses the banner Rounding Rule and the banner Rounding Rule: 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, if you want to retain two decimal places for X, you can use Math. round (X * 100)/100. for processing.

Ps: Rounding: Comparison between the JavaScript toFixed () method and Math. Round Method

Math.Round  (vs2005)Math.Round(3.44, 1); //Returns 3.4.Math.Round(3.45, 1); //Returns 3.4.Math.Round(3.46, 1); //Returns 3.5.

In the field of engineering, finance, and science, we often think that it is more accurate to round a decimal point to the nearest even number (rather than always round it up, that is, the "four homes and six inputs" method we call here.

The Banker algorithm used by the Math. Round () method is based on the IEEE Standard 754 international Standard.

The "four homes, six homes, and six homes" can be summarized as follows: "four homes, six homes, six homes, five homes, six homes, and five homes, six homes, before five, it is odd to enter one."

Summary

The above section describes the differences between using tofixed and round to process data in JavaScript code. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.