js Math.ceil()、Math.floor()和Math.round()

来源:互联网 发布:游族网络 刀剑乱舞 编辑:程序博客网 时间:2024/05/01 02:46

Math.ceil()、Math.floor()和Math.round()
这三个方法分别遵循下列舍入规则:
◎Math.ceil()执行向上舍入,即它总是将数值向上舍入为最接近的整数;
◎Math.floor()执行向下舍入,即它总是将数值向下舍入为最接近的整数;
◎Math.round()执行标准舍入,即它总是将数值四舍五入为最接近的整数(这也是我们在数学课上学到的舍入规则)。

alert(Math.ceil(25.9)); //26alert(Math.ceil(25.5)); //26alert(Math.ceil(25.1)); //26alert(Math.round(25.9)); //26alert(Math.round(25.5)); //26alert(Math.round(25.1)); //25alert(Math.floor(25.9)); //25alert(Math.floor(25.5)); //25alert(Math.floor(25.1)); //25
0 0