我们要将23.8转化成整数 有哪些方法呢
比如 Math.floor( ) 对数进行向下取整 它返回的是小于或等于函数参数,并且与之最接近的整数
Math.floor(5.1) 返回值 //5
Math.floor(5) 返回值 //5
Math.floor(-5.1) 返回值 //-6
Math.floor(-5.9) 返回值 //-6
Math.floor(23.8) 返回值 //23
Math.floor(-23.8) 返回值 //-24
Math.ceil( ) 对数进行向上取整 它返回的是大于或等于函数参数,并且与之最接近的整数。
Math.ceil(5) 返回值 //5
Math.ceil(5.1) 返回值 //6
Math.ceil(5.8) 返回值 //6
Math.ceil(-4) 返回值 //-4
Math.ceil(-4.1) 返回值 //-4
Math.ceil(-4.8) 返回值 //-4
Math.round( ) 对数进行四舍五入 再此不再详细举例 应该比较好理解
我们有更快捷的方法可以使用 那就是位或运算符号 | 可以当做是死套路 记住就好 举一个例子
23.95 | 0 返回值 //23
-23.95 | 0 返回值 //-23
看懂了么
当然 | 运算使用时 需要知道作用的数是 正数还是负数 正数就向下取整 负数是向上取整 谨记!!!