最近發現JS當中toFixed()方法存在一些問題,采用原生的Number對象的原型對象上的toFixed()方法時,規則並不是所謂的“四舍五入”或者是“四舍六入五成雙”,所謂“四舍六入五成雙”,在百度百科上給的解釋是:也即“4舍6入5湊偶”這里“四”是指≤4 時舍去,"六"是指≥6時進上,"五"指的是根據5后面的數字來定,當5后有數時,舍5入1;當5后無有效數字時,需要分兩種情況來講:①5前為奇數,舍5入1;②5前為偶數,舍5不進。(0是最小的偶數) 。百度百科上涉及的幾個例子在實際情況下確實成立,但不科學,並不能覆蓋所有的情況。
測試瀏覽器:屌絲瀏覽器IE6以及高級屌絲瀏覽器IE78(此處為方便未使用原生IE678,不過IETester破天荒地表現良好,精確做法應該是一個版本對應一個虛擬機來測試)和所有現代主流瀏覽器包括IE9、IE10、FF、Chrome、Opera、Safari。(注:在使用IE10的類似firebug的開發工具時,采用兼容IE低版本瀏覽器模式時的測試結果跟使用原生低版本IE瀏覽器的測試結果不一致)
在浮點數末尾≤4或者≥6的情況下的舍入沒有爭議,但當末尾正好等於5的情況下可謂混亂之極。
一、FF 穩定版測試結果如下所示:
二、FF Aurora測試結果如下所示:
三、FF Nightly的測試結果如下所示:
四、Chrome穩定版的測試結果如下所示:
五、Chromium的測試結果如下所示:
六、Chrome Canary的測試結果如下所示:
七、Opera穩定版的測試結果如下所示:
八、Opera Next的測試結果如下所示:
九、Safari的測試結果如下所示:
十、IE6-8的測試結果如下所示:
十一、IE9、10的測試結果如下所示:
總結:眾所周知,遵循IEEE754數值格式的語言的浮點計算會出現精度損耗的通病,ES也並非獨此一家,因此盡量不要進行某個特定浮點數值的測試,如:0.1+0.2;
解決方案:重寫Number.prototype.toFixed()方法:

1 <html> 2 <head> 3 <script type="text/javascript"> 4 Number.prototype.toFixed=function (d) { 5 var s=this+""; 6 if(!d)d=0; 7 if(s.indexOf(".")==-1)s+="."; 8 s+=new Array(d+1).join("0"); 9 if(new RegExp("^(-|\\+)?(\\d+(\\.\\d{0,"+(d+1)+"})?)\\d*$").test(s)){ 10 var s="0"+RegExp.$2,pm=RegExp.$1,a=RegExp.$3.length,b=true; 11 if(a==d+2){ 12 a=s.match(/\d/g); 13 if(parseInt(a[a.length-1])>4){ 14 for(var i=a.length-2;i>=0;i--){ 15 a[i]=parseInt(a[i])+1; 16 if(a[i]==10){ 17 a[i]=0; 18 b=i!=1; 19 }else break; 20 } 21 } 22 s=a.join("").replace(new RegExp("(\\d+)(\\d{"+d+"})\\d$"),"$1.$2"); 23 24 }if(b)s=s.substr(1); 25 return (pm+s).replace(/\.$/,""); 26 }return this+""; 27 28 }; 29 </script> 30 </head> 31 <body> 32 <input type="button" value="顯示0.009.toFixed(2)" onclick="alert(0.009.toFixed(2))"><br /> 33 <input type="button" value="顯示0.123.toFixed(2)" onclick="alert(0.123.toFixed(2))"><br /> 34 <input type="button" value="顯示0.125.toFixed(2)" onclick="alert(0.125.toFixed(2))"><br /> 35 <input type="button" value="顯示0.126.toFixed(2)" onclick="alert(0.126.toFixed(2))"><br /> 36 <input type="button" value="顯示20.445.toFixed(2)" onclick="alert(20.445.toFixed(2))"><br /> 37 <input onclick="alert(20.405.toFixed(2))" type="button" value="顯示20.405.toFixed(2)"> <br /> 38 <input onclick="alert(20.415.toFixed(2))" type="button" value="顯示20.415.toFixed(2)"> <br /> 39 <input onclick="alert(20.425.toFixed(2))" type="button" value="顯示20.425.toFixed(2)"> <br /> 40 <input onclick="alert(20.435.toFixed(2))" type="button" value="顯示20.435.toFixed(2)"> <br /> 41 <input onclick="alert(20.445.toFixed(2))" type="button" value="顯示20.445.toFixed(2)"> <br /> 42 <input onclick="alert(20.455.toFixed(2))" type="button" value="顯示20.455.toFixed(2)"> <br /> 43 <input onclick="alert(20.465.toFixed(2))" type="button" value="顯示20.465.toFixed(2)"> <br /> 44 <input onclick="alert(20.475.toFixed(2))" type="button" value="顯示20.475.toFixed(2)"> <br /> 45 <input onclick="alert(20.485.toFixed(2))" type="button" value="顯示20.485.toFixed(2)"> <br /> 46 <input onclick="alert(20.495.toFixed(2))" type="button" value="顯示20.495.toFixed(2)"> <br /> 47 <input onclick="alert(0.05.toFixed(1))" type="button" value="顯示0.05.toFixed(1)"> <br /> 48 <input onclick="alert(0.15.toFixed(1))" type="button" value="顯示0.15.toFixed(1)"> <br /> 49 <input onclick="alert(0.25.toFixed(1))" type="button" value="顯示0.25.toFixed(1)"> <br /> 50 <input onclick="alert(0.35.toFixed(1))" type="button" value="顯示0.35.toFixed(1)"> <br /> 51 <input onclick="alert(0.45.toFixed(1))" type="button" value="顯示0.45.toFixed(1)"> <br /> 52 <input onclick="alert(0.55.toFixed(1))" type="button" value="顯示0.55.toFixed(1)"> <br /> 53 <input onclick="alert(0.65.toFixed(1))" type="button" value="顯示0.65.toFixed(1)"> <br /> 54 <input onclick="alert(0.75.toFixed(1))" type="button" value="顯示0.75.toFixed(1)"> <br /> 55 <input onclick="alert(0.85.toFixed(1))" type="button" value="顯示0.85.toFixed(1)"> <br /> 56 <input onclick="alert(0.95.toFixed(1))" type="button" value="顯示0.95.toFixed(1)"> <br /> 57 </body> 58 </html>