最近發現JS當中toFixed()方法存在一些問題,採用原生的Number對象的原型對象上的toFixed()方法時,規則並非所謂的「四捨五入」或者是「四捨六入五成雙」,所謂「四捨六入五成雙」,在百度百科上給的解釋是:也即「4舍6入5湊偶」這裏「四」是指≤4 時捨去,"六"是指≥6時進上,"五"指的是根據5後面的數字來定,當5後有數時,舍5入1;當5後無有效數字時,須要分兩種狀況來說:①5前爲奇數,舍5入1;②5前爲偶數,舍5不進。(0是最小的偶數) 。百度百科上涉及的幾個例子在實際狀況下確實成立,但不科學,並不能覆蓋全部的狀況。javascript
測試瀏覽器:屌絲瀏覽器IE6以及高級屌絲瀏覽器IE78(此處爲方便未使用原生IE678,不過IETester破天荒地表現良好,精確作法應該是一個版本對應一個虛擬機來測試)和全部現代主流瀏覽器包括IE九、IE十、FF、Chrome、Opera、Safari。(注:在使用IE10的相似firebug的開發工具時,採用兼容IE低版本瀏覽器模式時的測試結果跟使用原生低版本IE瀏覽器的測試結果不一致)html
在浮點數末尾≤4或者≥6的狀況下的舍入沒有爭議,但當末尾正好等於5的狀況下可謂混亂之極。java
1、FF 穩定版測試結果以下所示:瀏覽器
2、FF Aurora測試結果以下所示:工具
3、FF Nightly的測試結果以下所示:開發工具
4、Chrome穩定版的測試結果以下所示:測試
5、Chromium的測試結果以下所示:this
6、Chrome Canary的測試結果以下所示:prototype
7、Opera穩定版的測試結果以下所示:3d
8、Opera Next的測試結果以下所示:
9、Safari的測試結果以下所示:
10、IE6-8的測試結果以下所示:
11、IE九、10的測試結果以下所示:
總結:衆所周知,遵循IEEE754數值格式的語言的浮點計算會出現精度損耗的通病,ES也並不是獨此一家,所以儘可能不要進行某個特定浮點數值的測試,如:0.1+0.2;
解決方案:重寫Number.prototype.toFixed()方法:
<html> <head> <script type="text/javascript"> Number.prototype.toFixed=function (d) { var s=this+""; if(!d)d=0; if(s.indexOf(".")==-1)s+="."; s+=new Array(d+1).join("0"); if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+(d+1)+"})?)\\d*$").test(s)){ var s="0"+RegExp.$2,pm=RegExp.$1,a=RegExp.$3.length,b=true; if(a==d+2){ a=s.match(/\d/g); if(parseInt(a[a.length-1])>4){ for(var i=a.length-2;i>=0;i--){ a[i]=parseInt(a[i])+1; if(a[i]==10){ a[i]=0; b=i!=1; }else break; } } s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2"); }if(b)s=s.substr(1); return (pm+s).replace(/\.$/,""); }return this+""; }; </script> </head> <body> <input type="button" value="顯示0.009.toFixed(2)" onclick="alert(0.009.toFixed(2))"><br /> <input type="button" value="顯示0.123.toFixed(2)" onclick="alert(0.123.toFixed(2))"><br /> <input type="button" value="顯示0.125.toFixed(2)" onclick="alert(0.125.toFixed(2))"><br /> <input type="button" value="顯示0.126.toFixed(2)" onclick="alert(0.126.toFixed(2))"><br /> <input type="button" value="顯示20.445.toFixed(2)" onclick="alert(20.445.toFixed(2))"><br /> <input onclick="alert(20.405.toFixed(2))" type="button" value="顯示20.405.toFixed(2)"> <br /> <input onclick="alert(20.415.toFixed(2))" type="button" value="顯示20.415.toFixed(2)"> <br /> <input onclick="alert(20.425.toFixed(2))" type="button" value="顯示20.425.toFixed(2)"> <br /> <input onclick="alert(20.435.toFixed(2))" type="button" value="顯示20.435.toFixed(2)"> <br /> <input onclick="alert(20.445.toFixed(2))" type="button" value="顯示20.445.toFixed(2)"> <br /> <input onclick="alert(20.455.toFixed(2))" type="button" value="顯示20.455.toFixed(2)"> <br /> <input onclick="alert(20.465.toFixed(2))" type="button" value="顯示20.465.toFixed(2)"> <br /> <input onclick="alert(20.475.toFixed(2))" type="button" value="顯示20.475.toFixed(2)"> <br /> <input onclick="alert(20.485.toFixed(2))" type="button" value="顯示20.485.toFixed(2)"> <br /> <input onclick="alert(20.495.toFixed(2))" type="button" value="顯示20.495.toFixed(2)"> <br /> <input onclick="alert(0.05.toFixed(1))" type="button" value="顯示0.05.toFixed(1)"> <br /> <input onclick="alert(0.15.toFixed(1))" type="button" value="顯示0.15.toFixed(1)"> <br /> <input onclick="alert(0.25.toFixed(1))" type="button" value="顯示0.25.toFixed(1)"> <br /> <input onclick="alert(0.35.toFixed(1))" type="button" value="顯示0.35.toFixed(1)"> <br /> <input onclick="alert(0.45.toFixed(1))" type="button" value="顯示0.45.toFixed(1)"> <br /> <input onclick="alert(0.55.toFixed(1))" type="button" value="顯示0.55.toFixed(1)"> <br /> <input onclick="alert(0.65.toFixed(1))" type="button" value="顯示0.65.toFixed(1)"> <br /> <input onclick="alert(0.75.toFixed(1))" type="button" value="顯示0.75.toFixed(1)"> <br /> <input onclick="alert(0.85.toFixed(1))" type="button" value="顯示0.85.toFixed(1)"> <br /> <input onclick="alert(0.95.toFixed(1))" type="button" value="顯示0.95.toFixed(1)"> <br /> </body> </html>
文章轉載:http://www.cnblogs.com/gushen/archive/2012/11/20/2778324.html#undefined