3

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.

sush
  • 476
  • 1
  • 7
  • 20

4 Answers4

4

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.

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.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
3

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:

// 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

jordancpaul
  • 2,954
  • 1
  • 18
  • 27
  • You link talks about "using the default locale", does closure actually detect the users locale and select the appropriate number formats? Or is the default locale always the same? – Matt Burland Mar 16 '12 at 19:14
  • 1
    Google's internationalization tools are static. There is no way to detect the language at run time and select the translation to use. That said, look in 'numberformatsymbols.js' and you will see: `goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;` in otherwords, it defaults to English. Right after this you will find: `if (goog.LOCALE == 'aa') { goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_aa; }` If you look in 'base.js' `goog.LOCALE = 'en'; // default to en` this is a compile-time define statement to control the language to use – jordancpaul Mar 16 '12 at 22:15
  • One other thing. If you need dynamic language capabilities, there are other tools which exist. Check out [http://jsgettext.berlios.de/](http://jsgettext.berlios.de/) to perform the dynamic translation and [https://github.com/AndyStricker/gettext-javascript](https://github.com/AndyStricker/gettext-javascript) to scan your source to find translatable strings. You should familiarize yourself with how [gettext](http://www.gnu.org/software/gettext/manual/gettext.html) works – jordancpaul Mar 16 '12 at 22:26
1

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

Mihai Nita
  • 5,547
  • 27
  • 27
  • jQuery globalize appears to have changed significantly since you wrote your comment. Currently (2014) only date formatting is supported – Chris Adams Feb 04 '14 at 16:00
0

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.

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
commonpike
  • 10,499
  • 4
  • 65
  • 58