Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Math round () in JavaScript Math object

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report