Javascript Number And Currency Localization
Solution 1:
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...newIntl.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
Solution 2:
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)"
}));
Solution 3:
Microsoft has created a useful plugin for jquery:
http://weblogs.asp.net/scottgu/archive/2010/06/10/jquery-globalization-plugin-from-microsoft.aspx
Solution 4:
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.
Post a Comment for "Javascript Number And Currency Localization"