问题
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