用残留物汇总数字

时间:2013-04-22 17:01:08

标签: javascript jquery math

此代码仅对大于或等于5的子值进行舍入:

Math.round(2.5) = 3
Math.round(2.4) = 2

我想得到以下内容:

Math.round(2.0000000001) = 3
Math.round(2.0) = 2

如何使用非零残差对任何整数进行舍入?

3 个答案:

答案 0 :(得分:5)

您应该使用Math.ceil

Math.ceil(2.0000000001) = 3

答案 1 :(得分:2)

使用Math.ceil

Math.ceil(2.0000000001) === 3; // true

但请注意,有些数字不会在内部表示为大于2的数字,即使它们看起来像:

Math.ceil(2.000000000000001) === 3;  // true
Math.ceil(2.0000000000000001) === 3; // false

答案 2 :(得分:1)

尝试Math.ceil - Math.ceil(2.0000000001)

  

ceil()方法将数字UPWARDS舍入到最接近的整数

Demo

相关问题