javascript - how to round to nearest integer [duplicate]

99封情书 提交于 2020-06-10 02:35:07

问题


Possible Duplicate:
How can I round of to whole numbers in JavaScript?

In javascript is there a function which allows me to round to the nearest integer? Either up or down.

So:

2.1 = 2
2.9 = 3
4.5 = 5
1.1 = 1


回答1:


Use Math.round(number):

var i = Math.round(2.1);



回答2:


Math.round(x) and Math.floor(x)

both documented here




回答3:


use Math.round()

EDIT:

Mine was wrong the one above me is correct



来源:https://stackoverflow.com/questions/7172835/javascript-how-to-round-to-nearest-integer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!