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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8906567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:56:30  来源:igfitidea点击:

Internationalization(Number formatting "num.toLocaleString()") not working for chrome

javascriptinternationalization

提问by sush

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.

我想在 Javascript 中进行数字格式化.. 我使用以下方法 num.toLocaleString() 这将适用于 Firefox, IE 但不适用于谷歌浏览器.. Wat 我需要添加它在 chrome 浏览器中工作。

回答by Jukka K. Korpela

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.

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

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 codeswhen specifying the locale.

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

回答by jordancpaul

Internationalization is always challenging and unfortunately there doesn't seem to be a consistent/pervasive solution to it. Your best bet is to use a 3rd party library to take care of things for you. We rely heavily on googles closure library, which has some pretty powerful i18n (internationalization) tools. Take a look at http://www.daveoncode.com/2009/11/26/goog-i18n-numberformat-formatting-number-locale-string/for an example of how to use it. In the end, it becomes as easy as:

国际化总是充满挑战,不幸的是似乎没有一致/普遍的解决方案。您最好的选择是使用 3rd 方库来为您处理事情。我们非常依赖谷歌的闭包库,它有一些非常强大的 i18n(国际化)工具。查看http://www.daveoncode.com/2009/11/26/goog-i18n-numberformat-formatting-number-locale-string/以了解如何使用它的示例。最后,它变得如此简单:

// define italian number format symbols 
goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_it_IT; 

// create new decimal formatter (PERCENT, CURRENCY, SCIENTIFIC are options)
formatter = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);

// view formatted and localized string
alert(formatter.format(15650.579));

If you are new to closure, don't worry. It's not hard to get set up and has a multitude of excellent helper classes that you may find useful. http://code.google.com/closure/library/docs/gettingstarted.html

如果您不熟悉关闭,请不要担心。设置起来并不难,并且有许多优秀的帮助类,您可能会发现它们很有用。 http://code.google.com/closure/library/docs/gettingstarted.html

回答by Mihai Nita

The JavaScript internationalization support is quite poor (as you have discovered). You might take a look at https://github.com/jquery/globalizeIt handles number formatting, and also dates, times, currencies.

JavaScript 国际化支持很差(如您所见)。你可以看看https://github.com/jquery/globalize它处理数字格式,还有日期、时间、货币。

回答by commonpike

A bit of voodoo can implement your own number formatting. You could build this into String.prototype, but I didnt want that, since its localized.

一点伏都教可以实现您自己的数字格式。你可以将它构建到 String.prototype 中,但我不想要那个,因为它是本地化的。

function reverse(str) {
    return str.split('').reverse().join(''); 
}

function num2str(num) {
    var str = num+"";
    // european
    // return reverse(reverse(str.replace('.',',')).replace(/\d{3}/g,'$&.').replace(/\.$/,''));
    // american
    return reverse(reverse(str).replace(/\d{3}/g,'$&,').replace(/\,$/,''));
}

and then its

然后它的

> console.log(25000.45)
> 25,000.45