值 | Math.ceil() | Math.floor() | Math.round() |
1.2 | 2 | 1 | 1 |
1.5 | 2 | 1 | 2 |
1.7 | 2 | 1 | 2 |
-1.2 | -1 | -2 | -1 |
-1.5 | -1 | -2 | -1 |
-1.7 | -1 | -2 | -2 |
Math.ceil()向上取整, Math.floor()向下取整, Math.round()四舍五入。 但是其算法为Math.floor(x + 0.5),所以负数的0.5是舍的。
值 | Math.ceil() | Math.floor() | Math.round() |
1.2 | 2 | 1 | 1 |
1.5 | 2 | 1 | 2 |
1.7 | 2 | 1 | 2 |
-1.2 | -1 | -2 | -1 |
-1.5 | -1 | -2 | -1 |
-1.7 | -1 | -2 | -2 |
Math.ceil()向上取整, Math.floor()向下取整, Math.round()四舍五入。 但是其算法为Math.floor(x + 0.5),所以负数的0.5是舍的。