gpt4 book ai didi

javascript - Number().toLocaleString() 在不同的浏览器中有不同的格式

转载 作者:bug小助手 更新时间:2023-10-28 10:49:49 27 4
gpt4 key购买 nike

我将 float 格式化为语言环境字符串(欧元),每个浏览器中的结果都大不相同。没有自己的功能可以修复吗?

var sum=2282.0000;
var formated_sum = Number(sum.toFixed(2)).toLocaleString("de-DE", {style: "currency", currency: "EUR"});

Firefox 结果:2.282,00 €

Chrome 结果:2.282 €

IE 结果:2.282,00 €

Safari 结果:2282 欧元

Safari 的结果非常错误,chrome 的结果并没有那么糟糕。任何想法如何在不编写自己的格式化函数的情况下解决这个问题?


这个问题在这里可能已经有了答案:toLocaleString() 在不同浏览器中的行为不一致不,我的问题不同,因为我正在寻找货币的解决方案,而不是日期

最佳答案

ECMA 262指定函数依赖于实现并且不接受任何参数。

Produces a String value that represents this Number value formattedaccording to the conventions of the host environment’s current locale.This function is implementation-dependent, and it is permissible, butnot encouraged, for it to return the same thing as toString.

NOTE The first parameter to this function is likely to be used in afuture version of this standard; it is recommended thatimplementations do not use this parameter position for anything else.

它也在 ECMA internationalization API specification (对于 Number.prototype.toLocaleString 取代 ECMA 262 但接受 2 个参数)

This definition supersedes the definition provided in ES5, 15.7.4.3.

When the toLocaleString method is called with optional argumentslocales and options, the following steps are taken:

Let x be this Number value (as defined in ES5, 15.7.4). If locales isnot provided, then let locales be undefined. If options is notprovided, then let options be undefined. Let numberFormat be theresult of creating a new object as if by the expression newIntl.NumberFormat(locales, options) where Intl.NumberFormat is thestandard built-in constructor defined in 11.1.3. Return the result ofcalling the FormatNumber abstract operation (defined in 11.3.2) witharguments numberFormat and x. The value of the length property of thetoLocaleString method is 0.

此外,mdn指定 Safari 不支持它。

关于可行的解决方案,请参阅 this answer就这样

关于javascript - Number().toLocaleString() 在不同的浏览器中有不同的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29942231/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com