JS中toFixed 方法的問題及解決方案

2022-03-25 06:07:42 字數 4431 閱讀 7761

測試瀏覽器:屌絲瀏覽器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

23132

"button

" value="

顯示0.009.tofixed(2)

" onclick="

alert(0.009.tofixed(2))

">33"

button

" value="

顯示0.123.tofixed(2)

" onclick="

alert(0.123.tofixed(2))

">34"

button

" value="

顯示0.125.tofixed(2)

" onclick="

alert(0.125.tofixed(2))

">35"

button

" value="

顯示0.126.tofixed(2)

" onclick="

alert(0.126.tofixed(2))

">36"

button

" value="

顯示20.445.tofixed(2)

" onclick="

alert(20.445.tofixed(2))

">37"

alert(20.405.tofixed(2))

" type="

button

" value="

顯示20.405.tofixed(2)

"> 38"

alert(20.415.tofixed(2))

" type="

button

" value="

顯示20.415.tofixed(2)

"> 39"

alert(20.425.tofixed(2))

" type="

button

" value="

顯示20.425.tofixed(2)

"> 40"

alert(20.435.tofixed(2))

" type="

button

" value="

顯示20.435.tofixed(2)

"> 41"

alert(20.445.tofixed(2))

" type="

button

" value="

顯示20.445.tofixed(2)

"> 42"

alert(20.455.tofixed(2))

" type="

button

" value="

顯示20.455.tofixed(2)

"> 43"

alert(20.465.tofixed(2))

" type="

button

" value="

顯示20.465.tofixed(2)

"> 44"

alert(20.475.tofixed(2))

" type="

button

" value="

顯示20.475.tofixed(2)

"> 45"

alert(20.485.tofixed(2))

" type="

button

" value="

顯示20.485.tofixed(2)

"> 46"

alert(20.495.tofixed(2))

" type="

button

" value="

顯示20.495.tofixed(2)

"> 47"

alert(0.05.tofixed(1))

" type="

button

" value="

顯示0.05.tofixed(1)

"> 48"

alert(0.15.tofixed(1))

" type="

button

" value="

顯示0.15.tofixed(1)

"> 49"

alert(0.25.tofixed(1))

" type="

button

" value="

顯示0.25.tofixed(1)

"> 50"

alert(0.35.tofixed(1))

" type="

button

" value="

顯示0.35.tofixed(1)

"> 51"

alert(0.45.tofixed(1))

" type="

button

" value="

顯示0.45.tofixed(1)

"> 52"

alert(0.55.tofixed(1))

" type="

button

" value="

顯示0.55.tofixed(1)

"> 53"

alert(0.65.tofixed(1))

" type="

button

" value="

顯示0.65.tofixed(1)

"> 54"

alert(0.75.tofixed(1))

" type="

button

" value="

顯示0.75.tofixed(1)

"> 55"

alert(0.85.tofixed(1))

" type="

button

" value="

顯示0.85.tofixed(1)

"> 56"

alert(0.95.tofixed(1))

" type="

button

" value="

顯示0.95.tofixed(1)

">

5758

JS當中toFixed 方法5不進製問題處理

最近發現js當中tofixed 方法存在一些問題,採用原生的number物件的原型物件上的tofixed 方法時,規則並不是所謂的 四捨五入 或者是 四捨六入五成雙 以谷歌瀏覽器為例 alert 1.5451.tofixed 2 輸出 1.55 alert 1.545.tofixed 2 輸出 1....

JS當中toFixed 方法5不進製問題處理

最近發現js當中tofixed 方法存在一些問題,採用原生的number物件的原型物件上的tofixed 方法時,規則並不是所謂的 四捨五入 或者是 四捨六入五成雙 以谷歌瀏覽器為例 在浮點數末尾 4或者 6的情況下的捨入沒有爭議,但當末尾正好等於5的情況下可謂混亂之極 與瀏覽器有關,同樣的方法,在...

JS中的toFixed 四捨五入方法

方法一 在js中四捨五入的函式 tofixed n n為要保留的小數字數。n為0 20,當n超過20的時候,js會出錯。如果小數點前和要擷取的前一位都是0時,不會按常理擷取。var h 0.07 h.tofixed 1 的值為0.0其實要解決這個問題,原來是不一定要重寫js中的number型別的to...