开发者

javascript - how to round to nearest integer [duplicate]

开发者 https://www.devze.com 2023-03-30 02:19 出处:网络
This question already has answers here: Closed 11 years ago. Possible Dupl开发者_如何转开发icate:
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

精彩评论

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