Number().toLocaleString() has different format in different browsers

。_饼干妹妹 提交于 2019-11-28 12:13:09
axelduch

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!