Number().toLocaleString() 在不同的浏览器中有不同的格式
我将浮点数格式化为语言环境字符串(欧元),每个浏览器中的结果都大不相同.没有自己的功能可以修复吗?
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 结果:2.282,00 €
Firefox result: 2.282,00 €
Chrome 结果:2.282 €
Chrome result: 2.282 €
IE 结果:2.282,00 €
IE result: 2.282,00 €
Safari 结果:2282 欧元
Safari result: 2282 €
Safari 的结果非常错误,chrome 的结果并没有那么糟糕.任何想法如何在不编写自己的格式化函数的情况下解决这个问题?
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?
这个问题在这里可能已经有了答案: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 指定函数依赖于实现并且不带参数.
ECMA 262 specifies that the function is implementation dependent and takes no arguments.
产生一个字符串值,表示此数字值格式化根据宿主环境当前语言环境的约定.这个函数是依赖于实现的,它是允许的,但是不鼓励,因为它返回与 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.
注意此函数的第一个参数可能用于本标准的未来版本;建议实现不会将此参数位置用于其他任何事情.
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 规范中(其中Number.prototype.toLocaleString
取代 ECMA 262 但接受 2 个参数)
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.
当使用可选参数调用 toLocaleString 方法时语言环境和选项,采取以下步骤:
When the toLocaleString method is called with optional arguments locales and options, the following steps are taken:
让 x 是这个 Number 值(在 ES5, 15.7.4 中定义).如果语言环境是未提供,则让语言环境未定义.如果选项不是提供,然后让选项未定义.让 numberFormat 为创建一个新对象的结果,就像通过表达式 newIntl.NumberFormat(locales, options) 其中 Intl.NumberFormat 是11.1.3 中定义的标准内置构造函数.返回结果调用 FormatNumber 抽象操作(在 11.3.2 中定义)参数 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
相关文章