开发者

javascript - how to round to nearest integer [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Dupl开发者_如何转开发icate:

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


Use Math.round(number):

var i = Math.round(2.1);


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

both documented here


use Math.round()

EDIT:

Mine was wrong the one above me is correct

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜