Alex Rivera | Logout

Is there a functionality in JavaScript to convert values into specific locale formats?

Asked 2011-03-15T15:46:06.240
44

Is there a built in function of JavaScript to convert a string into a particular locale (Euro in my case)?

E.g. 50.00 should get converted to 50,00 €.

Edit
Report

1 Answer

4

Built-in, yes and no. There is Number.toLocaleString() but it is dependent on the system's locale.

However, there are some libraries which have modules for this. MooTools's Locale.Number for example allows you to convert a number into different locales (adding your own locale is trivial).

Locale.use("EU");
var inEuros = (50).formatCurrency(); // € 50,00

jsFiddle Demo


If you want the € sign to be printed after, you can simply create your own locale:

Locale.define('EU-suffix', 'Number', {
    currency: {
        suffix: ' €'
    }
}).inherit('EU', 'Number');
answered 2011-03-15T15:52:37.383

Your Answer