javaScript中小数取整,四种方法的比较


1.parseInt:只取整数位
例如:
parseInt(3.7) 取整结果为:3
parseInt(-1.1) 取整结果为:-1

2.Math.floor :向下去整,取大的整数
例如:
Math.floor(3.7) 取整结果为:4
Math.floor(-1.1) 取整结果为:-1


3.Math.ceil :向上去整,取小的整数
例如:
Math.floor(3.7) 取整结果为:3
Math.floor(-1.1) 取整结果为:-2

4.Math.round:四舍五入
例如:
Math.round(3.3) 取整结果为:3
Math.round(3.5) 取整结果为:4
Math.round(3.7) 取整结果为:4
Math.round(-1.3) 取整结果为:-1
Math.round(-1.5) 取整结果为:-1
Math.round(-1.6) 取整结果为:-2

原文地址:https://www.cnblogs.com/caozong/p/5772453.html