How to round html pages, JS uses tofixed and round to deal with the difference between rounding data Develop Paper

1. Tofixed method

The toFixed() method rounds a Number to the specified number of decimal places. For example, if the data Num is kept to 2 decimal places, it is expressed as: toFixed(Num); but its rounding rules are different from those in mathematics. The banker's rounding rule is used, and the banker's rounding: the so-called banker's rounding method , its essence is a method of rounding to five and taking even (also known as rounding to five to leave doubles). The specific rules are as follows:

To put it simply, it is: round up to five. If the number after five is non-zero, add one. If the number after five is zero, it should be rounded up. If the number before five is even, it should be rounded up.

Obviously this rule doesn't fit the way we usually deal with data. In order to solve such a problem, you can customize the Math.round method to specify how many bits of data to keep for processing.

2, round method

The round() method rounds a number to the nearest integer. For example: Math.round(x), it will take x to its nearest integer. The method of choosing and rounding uses the method of rounding, which conforms to the rules of choosing and rounding in mathematics. The processing of decimals is not so convenient, but it can be customized according to different requirements.

For example: To process X with two decimal places, you can use Math.round(X * 100) / 100. for processing.

ps: rounding: JavaScript toFixed() method and Math.Round method comparison

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 engineering, finance, and science, we tend to think that it is more precise to round a decimal to the nearest even number (rather than always round it up), which is what we call "rounding" here Law.

The basis of the banker's algorithm used by the Math.Round() method is the international standard IEEE Standard 754.

The "rounding method" can be summarized as follows: "Round up to five. If the number after five is not zero, add one. If the number after five is zero, it should be considered odd and even. If it is even before five, it should be rounded up. If it is odd before five, it should be added by one. "

Summarize

The above is the support of the developer website introduced by Xiaobian to you!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324325937&siteId=291194637