Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand Math round () in JavaScript Math object

Shulou Source: shulou.com Published: 2022-06-03 08:12:26 10月02日 Update

Today, I will talk to you about how to understand Math round () in JavaScript Math objects. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Math.round () in js is a numerical method of "rounding", which is understood in msdn and w3school documents as rounding a number to the nearest integer. In our practical application, it is understandable that the result of math.round (- 11.6) is-12, but why is the return value of math.round (- 11.5)-11 instead of-12?

How to understand Math.round ()

We can understand Math.floor (x = 0.5) in this way, that is, after adding 0.5 to the original number, it is rounded up according to the negative number x, and the integer is rounded up by x. So:

Math.round (- 11.5); / / it is understood as-11.5, 0.5, and the output of the whole result is-11.

Math.round (- 11.6); / / it is understood as-11.6, 0.5 percent, 11.1, and the whole result is output as-12.

Math.round (11.5); / / understood as 11.5, 0.5, 12, and rounded down, the output is 12.

Math.round (11.6); / / it is understood as 11.6, 0.5, 12.1, and the output of rounding down is 12.

Math.round () operation output

Case1: the first place after the decimal point = 5

/ / positive number:

Math.round (11.5); / / output 12

/ / negative number:

Math.round (- 11.5); / / output-11

Case2: first place 5 after the decimal point

/ / positive number:

Math.round (11.69); / / output 12

/ / negative number:

Math.round (- 11.69); / / output-12

Case4: if there are too many decimal places, precision will be involved and the result will not be accurate.

Math.round (- 10.500000000000001); / / output-11

Math.round (- 10.5000000000000001); / / output-10

After reading the above, do you have any further understanding of how to understand Math round () in the JavaScript Math object? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Output result decimal point negative content positive object number precision rounding digits actual numeric integer document method more knowledge article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL OPPO Reno Xiaomi Apple