国际化(数字格式“num.toLocaleString()")不适用于 chrome

2022-01-18 00:00:00 internationalization javascript

我想在 Javascript 中进行数字格式化.. 我使用以下方法num.toLocaleString() 适用于 Firefox、IE 但不适用于 Google Chrome..我需要添加它才能在 chrome 浏览器中工作.

i want do number formatting in Javascript.. and i use the following method num.toLocaleString() which will work for Firefox, IE but doesnt work for Google Chrome.. Wat i need to add for it work in chrome browser.

推荐答案

toLocaleString() 方法根据定义是依赖于实现的:它使用实现语言环境,例如浏览器语言环境.因此,如果我查看您使用该方法的页面,我会看到根据芬兰语或英语语言环境设置格式的数字,具体取决于我使用的浏览器.

The toLocaleString() method is by definition implementation-dependent: it uses the implementation locale, such as browser locale. So if I were looking at your page that uses the method, I would see numbers formatted according to Finnish or English locale, depending on which browser I’m using.

您想要的是按页面的语言环境进行本地化,为此您需要其他东西.在简单的情况下,您可能会自己编写代码,但数字格式通常很复杂,因此使用库是合理的,例如 全球化.查看一个简单的 demo 的精简源代码.在 Globalize 中,您在指定区域设置时使用 标准语言代码.

What you want is localization by the locale of the page, and for this you need something else. In simple cases you might code it yourself, but number formatting is in general complicated, making it reasonable to use a library, such as Globalize. Check out the compact source of a simple demo. In Globalize, you use standard language codes when specifying the locale.

相关文章