JavaScript 取整函数
JavaScript 有两个常用的取整函数:
-
Math.floor(x):返回小于或等于 x 的最大整数。
例如,Math.floor(4.5) 返回 4,Math.floor(-4.5) 返回 -5。 -
Math.ceil(x):返回大于或等于 x 的最小整数。
例如,Math.ceil(4.5) 返回 5,Math.ceil(-4.5) 返回 -4。
还有一个取整函数是 Math.round(x):将 x 四舍五入为最接近的整数。
例如,Math.round(4.5) 返回 5,Math.round(4.4) 返回 4。
该博文为原创文章,未经博主同意不得转。本文章博客地址:https://cplusplus.blog.youkuaiyun.com/article/details/134171556