4

This seems like a problem someone would've already solved in the year 2009, but I can't find a decent JavaScript library that will take a number like:

12345.58

And format it correctly based on a specific culture (ie, "nl-nl") or ISO currency code.

dojo showed promise, but it doesn't include many cultures by default, and actually wasn't working properly in my tests.

I basically want something that is 100% or near-100% equivalent to what .NET does for me, when I run:

String.Format([cultureInfo Object], "{0:N}", myValue) ' for numbers

and

String.Format([cultureInfo Object], "{0:C}", myValue) ' for currency

Nicholas Head
  • 3,716
  • 5
  • 27
  • 39

1 Answers1

5

The ASP.NET Ajax framework lets you do this.

Set the EnableScriptGlobalization property on the ScriptManager to true. You can then use the Number.localeFormat function to format numbers.

Greg
  • 16,540
  • 9
  • 51
  • 97
  • Thanks, that is pointing me in the right direction. Know anything like that for currency, though? I guess I could try to roll my own, with Number.localeFormat formatting the number.. – Nicholas Head Oct 26 '09 at 04:25
  • You should be able to just use "c", like `var currency = aNum.localeFormat("c");` – Greg Oct 26 '09 at 13:21
  • You can also specify the number of decimal digits. For example, "p4" will give you a percentage with 4 decimal places. – Greg Oct 26 '09 at 13:24