問題描述
我將浮點數(shù)格式化為語言環(huán)境字符串(歐元),每個瀏覽器中的結(jié)果都大不相同.沒有自己的功能可以修復(fù)嗎?
I format a float to a locale string (Euro) and there are very different results in every browser. Is it possible to fix without an own function?
var sum=2282.0000;
var formated_sum = Number(sum.toFixed(2)).toLocaleString("de-DE", {style: "currency", currency: "EUR"});
Firefox 結(jié)果:2.282,00 €
Firefox result: 2.282,00 €
Chrome 結(jié)果:2.282 €
Chrome result: 2.282 €
IE 結(jié)果:2.282,00 €
IE result: 2.282,00 €
Safari 結(jié)果:2282 歐元
Safari result: 2282 €
Safari 的結(jié)果非常錯誤,chrome 的結(jié)果并沒有那么糟糕.任何想法如何在不編寫自己的格式化函數(shù)的情況下解決這個問題?
Safari results are very much wrong, chrome results are not so much bad. Any Idea how to fix that without writing an own function for formatting?
這個問題在這里可能已經(jīng)有了答案:toLocaleString() 在不同瀏覽器中的行為不一致不,我的問題不同,因為我正在尋找貨幣的解決方案,而不是日期
This question may already have an answer here: Inconsistent behavior of toLocaleString() in different browser No, my question is different because i am searching for a solution for Currency, not DATE
推薦答案
ECMA 262 指定函數(shù)依賴于實現(xiàn)并且不帶參數(shù).
ECMA 262 specifies that the function is implementation dependent and takes no arguments.
產(chǎn)生一個字符串值,表示此數(shù)字值格式化根據(jù)宿主環(huán)境當(dāng)前語言環(huán)境的約定.這個函數(shù)是依賴于實現(xiàn)的,它是允許的,但是不鼓勵,因為它返回與 toString 相同的東西.
Produces a String value that represents this Number value formatted according to the conventions of the host environment’s current locale. This function is implementation-dependent, and it is permissible, but not encouraged, for it to return the same thing as toString.
注意此函數(shù)的第一個參數(shù)可能用于本標(biāo)準(zhǔn)的未來版本;建議實現(xiàn)不會將此參數(shù)位置用于其他任何事情.
NOTE The first parameter to this function is likely to be used in a future version of this standard; it is recommended that implementations do not use this parameter position for anything else.
它也在 ECMA 國際化 API 規(guī)范中(其中Number.prototype.toLocaleString
取代 ECMA 262 但接受 2 個參數(shù))
It is also in ECMA internationalization API specification (which for Number.prototype.toLocaleString
supersedes ECMA 262 but accepts 2 arguments)
此定義取代 ES5 15.7.4.3 中提供的定義.
This definition supersedes the definition provided in ES5, 15.7.4.3.
當(dāng)使用可選參數(shù)調(diào)用 toLocaleString 方法時語言環(huán)境和選項,采取以下步驟:
When the toLocaleString method is called with optional arguments locales and options, the following steps are taken:
讓 x 是這個 Number 值(在 ES5, 15.7.4 中定義).如果語言環(huán)境是未提供,則讓語言環(huán)境未定義.如果選項不是提供,然后讓選項未定義.讓 numberFormat 為創(chuàng)建一個新對象的結(jié)果,就像通過表達(dá)式 newIntl.NumberFormat(locales, options) 其中 Intl.NumberFormat 是11.1.3 中定義的標(biāo)準(zhǔn)內(nèi)置構(gòu)造函數(shù).返回結(jié)果調(diào)用 FormatNumber 抽象操作(在 11.3.2 中定義)參數(shù) numberFormat 和 x.長度屬性的值toLocaleString 方法為 0.
Let x be this Number value (as defined in ES5, 15.7.4). If locales is not provided, then let locales be undefined. If options is not provided, then let options be undefined. Let numberFormat be the result of creating a new object as if by the expression new Intl.NumberFormat(locales, options) where Intl.NumberFormat is the standard built-in constructor defined in 11.1.3. Return the result of calling the FormatNumber abstract operation (defined in 11.3.2) with arguments numberFormat and x. The value of the length property of the toLocaleString method is 0.
此外,mdn指定 Safari 不支持它.
Besides, mdn specifies that Safari has no support for it.
至于可行的解決方案,請參閱this answer on SO
As for a viable solution see this answer on SO
這篇關(guān)于Number().toLocaleString() 在不同的瀏覽器中有不同的格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!