`
xinghu
  • 浏览: 31827 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

js float 取精度

阅读更多

js里边取float数的精度的方法 toFixed()

 

toFixed() 方法可把 Number 四舍五入为指定小数位数的数字。

 

toFixed() 里面的数为 0- 20 之间的整数。

 

var a = 1.23456

 

a.toFixed(2) = 1.23

 

toFixed 方法有bug,不同的浏览器对它的解析不一样。比如,0.009.toFixed(2)在firefox下 为0.01 而在IE7下为

 

0.00。详见:http://www.cnblogs.com/MythYsJh/archive/2009/02/13/1389843.html

 

解决办法:

 

重写

Number.prototype.toFixed  =   function ( fractionDigits )
{

     return  (parseInt( this   *  Math.pow(  10 , fractionDigits  )  +   0.5 )/Math.pow(10,fractionDigts)).toString();
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics