用javascript做一个灵活四舍五入的函数,输入如12.987等,点击按钮后自动四舍五入成12.99,若是输入12.99,四舍五入后就成为13,另外,能控制保留小数位数,
如12.7686,保留三位小数位就是12.769,保留两位小数位就是12.77
如12.7686,保留三位小数位就是12.769,保留两位小数位就是12.77
<script language="javascript">
/*
* ForDight(Dight,How):数值格式化函数,Dight要
* 格式化的 数字,How要保留的小数位数。
*/
function ForDight(Dight,How)
{
Dight = Math.round (Dight*Math.pow(10,How))/Math.pow(10,How);
return Dight;
}
alert(ForDight(12345.67890,2));
</script>