日期:2014-05-16  浏览次数:20444 次

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是保留几位小数。
原理是先将数据放大然后缩小,会四舍五入,但不会自动补齐到想要的位数。