Jul
30
2013
js小数数字取整的几种方法
作者:
绝缘体.. 发布:
2013-07-30 10:06 分类:
未分类 阅读:
抢沙发
js实现小数数字取整的方法,主要用到三个js函数(Math.ceil,Math.round,Math.floor),具体使用方法及介绍如下:
/*
Math.ceil求最小的整数但不小于本身.
Math.round求本身的四舍五入。
Math.floor求最大的整数但不大于本身.
*/
var arg=3.26513098;
document.write('<br />');
document.write('原数字:'+arg);
document.write('<br />');
document.write('Math.ceil:'+Math.ceil(arg));//4
document.write('<br />');
document.write('Math.round:'+Math.round(arg));//3
document.write('<br />');
document.write('Math.floor:'+Math.floor(arg));//3
非常有意思的是PHP也有和上述一样功能的函数,函数名字都一样,具体可参见本站文章:php取整数值的几种方式的小结
另外关于如何使用javascript实现数字的四舍五入请参照本站文章:
js四舍五入
微信扫一扫,打赏作者吧~