javascript - 如何舍入到最接近的整数

时间:2011-08-24 08:55:23

标签: javascript math

  

可能重复:
  How can I round of to whole numbers in JavaScript?

在javascript中是否有一个函数允许我舍入到最接近的整数?向上或向下。

所以:

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

3 个答案:

答案 0 :(得分:37)

使用Math.round(number)

var i = Math.round(2.1);

答案 1 :(得分:4)

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

记录了here

答案 2 :(得分:0)

使用Math.round()

编辑:

我的错误是我的错误

相关问题