js小数数字取整的几种方法

 
更多

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四舍五入

打赏

本文固定链接: https://www.cxy163.net/archives/3929 | 绝缘体

该日志由 绝缘体.. 于 2013年07月30日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: js小数数字取整的几种方法 | 绝缘体
关键字: , , , ,

js小数数字取整的几种方法:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter