Number()。toLocaleString()在不同的浏览器中具有不同的格式 [英] Number().toLocaleString() has different format in different browsers

查看:182
本文介绍了Number()。toLocaleString()在不同的浏览器中具有不同的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个浮点格式化为一个语言环境字符串(欧元),并且在每个浏览器中都有非常不同的结果。有没有一个自己的函数可以修复?

  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€

p>

Safari的结果是非常错误的,chrome结果并没有那么糟糕。
任何想法如何解决这个问题,而不写一个自己的格式化功能?






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

解决方案


这个函数是依赖于实现的,这是允许的,但不鼓励
,因为它返回的东西和toString是一样的。



这个函数的第一个参数可能用在这个标准的
未来版本中,

它也位于 ECMA国际化API规范(对于 Number.prototype.toLocaleString 取代ECMA 262,但接受2个参数)
$ b


这个定义取代了ES5中提供的定义,15.7.4.3。

使用可选参数
语言环境和选项调用toLocaleString方法时,将采取以下步骤:



令x为这个数字值(如ES5,15.7.4所定义)。如果语言环境是
没有提供,那么让locales不确定。如果期权不是
,那么让期权不确定。让numberFormat成为创建一个新对象的结果
Intl.NumberFormat(locales,options)其中Intl.NumberFormat是11.1节定义的
标准内置构造函数0.3。返回
的结果,使用
参数numberFormat和x调用FormatNumber抽象操作(在11.3.2中定义)。
toLocaleString方法的length属性的值是0。

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



至于一个可行的解决方案请参阅这个答案在SO


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 result: 2.282,00 €

Chrome result: 2.282 €

IE result: 2.282,00 €

Safari result: 2282 €

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?


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 specifies that the function is implementation dependent and takes no arguments.

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.

It is also in ECMA internationalization API specification (which for Number.prototype.toLocaleString supersedes ECMA 262 but accepts 2 arguments)

This definition supersedes the definition provided in ES5, 15.7.4.3.

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

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.

Besides, mdn specifies that Safari has no support for it.

As for a viable solution see this answer on SO

这篇关于Number()。toLocaleString()在不同的浏览器中具有不同的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆