首先我们看以下几个方法:
- Math.ceil()用作向上取整。
- Math.floor()用作向下取整。
- Math.round() 我们数学中常用到的四舍五入取整。
console.log(Math.ceil(5.9)); //6
console.log(Math.ceil(5.1)); //6
console.log(Math.ceil(-5.9)); //-5
console.log(Math.ceil(-5.1)); //-5
console.log(Math.floor(5.9)); //5
console.log(Math.floor(5.1)); //5
console.log(Math.floor(-5.9)); //-6
console.log(Math.floor(-5.1)); //-6
console.log(Math.round(5.5)); //6
console.log(Math.round(5.1)); //5
console.log(Math.round(-5.6)); //-6
console.log(Math.round(-5.5)); //-5(对于 0.5,round()方法将进行上舍入)
console.log(Math.round(-5.1)); //-5