Javascript Number and Currency localization

Microsoft has created a useful plugin for jquery:

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


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)"
}));

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