方法一:
使用x | 0
,快速实现去除小数位保留整数。
console.log(99.55 | 0); // 99
console.log(33.55 | 0); // 33
方法二:
使用.tofixed(0)
,保留小数位,注意此方法会将数字四舍五入。
var number = 99.55;
console.log(number.tofixed(0)); // 100
console.log(number.tofixed(1)); // 99.6
方法三:
使用Math.floor()
向下取整。
Math.floor(99.55); // 99
Math.floor(-99.55); // -100