1.parseInt(xxx)的取整方式是截断取整
console.log(parseInt(1.1));//1
console.log(parseInt(1.9));//1
console.log(parseInt(-1.1));//-1
console.log(parseInt(-1.9));//-1
2.Math.ceil(xxx) 向上取整
console.log(Math.ceil(1.1));//2
console.log(Math.ceil(1.9));//2
console.log(Math.ceil(-1.1));//-1
console.log(Math.ceil(-1.9));//-1
3.Math.floor(xxx) 向下取整
console.log(Math.floor(1.1));//1
console.log(Math.floor(1.9));//1
console.log(Math.floor(-1.1));//-2
console.log(Math.floor(-1.9));//-2
4.Math.round(xxx) 四舍五入取整
console.log(Math.round(1.1));//1
console.log(Math.round(1.9));//2
console.log(Math.round(-1.1));//-1
console.log(Math.round(-1.9));//-2
补充:如果想要四舍五入保留一位小数,可以Math.round(xxx * 10) / 10
注意:js中的Math.round()方法和java中的类似,四舍五入规则都可以理解为先加0.5再向下取整,这就意味着Math.round(-0.5)的结果是0。
参考:
https://www.jianshu.com/p/a93bd02d9eb7
https://www.cnblogs.com/yhood/p/11447250.html
https://blog.csdn.net/m0_37170006/article/details/115011415