JS processing data rounding (detailed explanation of the difference between tofixed and round)

韦小宝
Release: 2018-01-17 09:56:59
Original
1946 people have browsed it

The following editor will bring you an articleJSprocessing data rounding (detailed explanation of the difference between tofixed and round). The editor thinks it’s pretty good, so I’ll share the js source code with you now and give it as a reference. If you are interested in js, follow the editor to take a look.

1, tofixed method

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

To put it simply: consider rounding up to five, if it is non-zero after five, add one, if it is zero after five, it will look at odd or even, if it is even before five, it should be discarded , It’s strange to make one more step before five.

Obviously this rule does not conform to the way we usually deal with data. In order to solve this problem, you can customize the implementation using the Math.round method to specify how many bits of data to retain for processing.

2, round method

round() method can round a number to the nearestinteger. For example: Math.round(x) takes x to the nearest integer. The rounding method is used in the rounding method, which conforms to the rules of 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.

The above JS processing data rounding (detailed explanation of the difference between tofixed and round) is all the content shared by the editor. I hope it can give you a reference! !

Related recommendations:

Example of password strength verification function implemented by JS based on regular expressions

Detailed explanation of js object instance (JavaScript object depth Analysis, deep understanding of js objects)

Detailed explanation of the six error types in JavaScript

The above is the detailed content of JS processing data rounding (detailed explanation of the difference between tofixed and round). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!