并非所有浏览器都支持 toLocaleString()?

2022-01-12 00:00:00 formatting javascript

我有这个简单的功能:

Chrome、Firefox、IE:

Chrome, Firefox, IE:

Number(1000000).toLocaleString()
"1 000 000" // in french system, the space is the separator instead of the comma

Opera,傲游:

Number(1000000).toLocaleString()
"1000000"

为什么 Opera 和 Maxthon 不能格式化?他们支持这种方法,但没有以正确的方式执行?

why Opera and Maxthon cant format it? they support this method but dont execute it in the right way?

是否有任何 toLocaleString() 替换?

推荐答案

语言规范 定义非常开放:

生成一个字符串值,该值表示根据宿主环境当前语言环境的约定格式化的数字值.此函数依赖于实现,允许但不鼓励它返回与 toString 相同的内容.

15.7.4.3 Number.prototype.toLocaleString()

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.

允许不同的浏览器以不同的方式实现它,并且可以根据用户选择的语言环境以不同的方式实现它.

Different browsers are allowed to implement it differently, and can implement it differently based on the locale chosen by the user.

相关文章