6

How can i format currency related data in a manner that is culture aware in JavaScript?

kjv
  • 10,587
  • 33
  • 98
  • 139

4 Answers4

2

Dojo has a currency formatter that's locale aware.

If you don't want to include Dojo in your project just for this function, then perhaps you can localize the currency in your back-end?

sherbang
  • 14,749
  • 1
  • 21
  • 16
  • 2
    I believe the Dojo currecy formatting is based on XML data from the Unicode Common Locale Data Repository (http://unicode.org/cldr). While Dojo will do the hard work for you, if you don't want to use it you can get the raw data from there. – georgebrock Sep 09 '08 at 10:54
2

So I know this is an old question, but incase anyone else shows up looking for similar answers, in modern JavaScript you can use

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number)

For more info here is the reference doc.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat

Hans
  • 79
  • 1
  • 8
1

Number.toLocaleString (implemented in JavaScript 1.5, ECMAScript 3rd Edition)

var number = 3500;
console.log(number.toLocaleString()); /* Displays "3,500" in English locale */

Docs on MDN.

laktak
  • 52,625
  • 16
  • 126
  • 156
-3

there is a Number.localeFormat function but I'm not sure it's what your after

http://msdn.microsoft.com/en-gb/library/bb310813.aspx

Christian Hagelid
  • 8,082
  • 4
  • 38
  • 63