听老歌 发表于 2012-01-11 10:44

js 四舍五入 方法

js 四舍五入 方法 function test(){

var a = 234234.44505;
alert(a.toFixed(2));

a = Math.round(a*Math.pow(10,How))/Math.pow(10,How);
alert(a);
} 一共两种方法:
toFixed(length) 参数表示要留取的小数位数,并且会自动补齐和四舍五入。

Math.round(a*Math.pow(10,How))/Math.pow(10,How);
a是代表原数,how是保留几位小数。
原理是先将数据放大然后缩小,会四舍五入,但不会自动补齐到想要的位数。

如果有一天21 发表于 2012-01-12 10:35

谢谢分享
页: [1]
查看完整版本: js 四舍五入 方法