js中常用小数舍入为整数的方法有几个:Math.ceil()、Math.floor()、Math.round()。
这三个方法分别遵循的规则是:
Math.ceil()执行向上舍入,即它总是将数值向上舍入为最近的整数;
Math.floorI()执行向下舍入,即它总是将值向下舍入为最接近的整数;
Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数。
下面是这些方法的例子:
alert(Math.ceil(25.9)); //26
alert(Math.ceil(25.5)); //26
alert(Math.ceil(25.1)); //26
alert(Math.floor(25.9)); //25
alert(Math.floor(25.5)); //25
alert(Math.floor(25.1)); //25
alert(Math.round(25.9)); //26
alert(Math.round(25.5)); //26
alert(Math.round(25.1)); //25
转载于:https://blog.51cto.com/eadonlearning/1334642