javascript四舍五入函数

如何用javascript做一个灵活四舍五入的函数,要求输入如12.987等,点击按钮后自动四舍五入成12.99,若是输入12.99,四舍五入后就成为13,另外,最好还能控制保留小数位数,
如12.7686,保留三位小数位就是12.769,保留两位小数位就是12.77,如此如此,请赐教!!

---------------------------------------------------------------

function cheng(num,n)
{var dd=1;
var tempnum;
for(i=0;i

1<n;i++) (dight*math.pow(10,how))="" *="" <="" \---------------------------------------------------------------="" alert(fordight(12345.67890,2));="" alert(tempnum="" dd);="" dd*="10;" dight="Math.round" dight;="" fordight(dight,how)="" fordight(dight,how):数值格式化函数,dight要="" function="" math.pow(10,how);="" return="" script="" tempnum="Math.round(tempnum);" {="" }="" 数字,how要保留的小数位数。="" 格式化的="" 里面的两个参数:num就是要转换的数据。n为要转换的位数=""></n;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus