17

I've run into numbers and currency localization in JavaScript

What I need is a convenient library for that.

I am responsible for setting the decimal separators, currency, etc.

Please post the best links you think

Dan
  • 55,715
  • 40
  • 116
  • 154
  • If you pass the separators and currency from django... what other library do you need? o.o – Khez Apr 11 '11 at 16:16
  • it is strange, but the best thing I've found ever is built in here http://www.ejschart.com/examples/formatters/number.html – Dan Apr 11 '11 at 16:18
  • @Khez: I need a client-side library written in JavaScript – Dan Apr 11 '11 at 16:18

4 Answers4

28

Most modern browsers have built in support internationalisation in the form of the global Intl object and extensions to Number, String & Date.

var money = 123456.12;

// display with correct formatting
money.toLocaleString('de-DE'); // "123.456,12"

// for currency, bad as we're leaving the precision to the gods of floating point numbers
money.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' }); // "£123,456.12"

// for currency, good as we're using strings...
new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format('12312.12')

If you're not familiar with why floating point numbers are bad for currency info check this out on floating point numbers

Ian Chadwick
  • 1,547
  • 1
  • 19
  • 21
4

The best answer for you probably depends on what javascript libary, if any, you are currently using. But YUI has support for number/currency formatting with internationalization, and it is a solid and well-designed library.

Example:

alert(Y.DataType.Number.format(123123123.176,{
    prefix: "€",
    thousandsSeparator: ".",
    decimalSeparator: ",",
    decimalPlaces: 2,
    suffix: " (EUR)"
}));
Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
4

Microsoft has created a useful plugin for jquery:

http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx

ScottE
  • 21,530
  • 18
  • 94
  • 131
1

This post is quite old, but I post a response in case it's interesting someone.

I found the numeral.js library pretty useful to do this.
You can define your custom formats and add localized files in the same way that 'moment.js'.

You should probably check to see if it fits your needs.

GBL
  • 386
  • 4
  • 13