0

This code rounds up only the subvalues bigger or equal to 5:

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

I would like to get the following:

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

How do I round up any integer with non-zero residue?

4

3 回答 3

5

你应该使用Math.ceil

Math.ceil(2.0000000001) = 3
于 2013-04-22T17:01:54.153 回答
2

使用Math.ceil

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

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

Math.ceil(2.000000000000001) === 3;  // true
Math.ceil(2.0000000000000001) === 3; // false
于 2013-04-22T17:02:13.077 回答
1

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

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

演示

于 2013-04-22T17:02:34.687 回答