0

I need to display a formatted number on a web page using JavaScript. I want to format it so that there are commas in the right places. How would I do this with a regular expression? I've gotten as far as something like this:

I am trying to use regular expression using JavaScript to format currency for both in English and in French. Based on my language it should use a particular regular expression. The English is working well but am trying to figure out the French.

return "$" + num.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');

This is working for English. So my 5000 get converted to $5,000.00 this is fine.

I need French to looks like this 5 000,00 $

Alex McMillan
  • 17,096
  • 12
  • 55
  • 88
Softwarex3
  • 33
  • 1
  • 1
  • 7
  • I'm not sure if regex is the right tool for that. But I guess this question will help: http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript – Marvin Jun 15 '15 at 14:49
  • http://stackoverflow.com/a/149099/4028085 – brso05 Jun 15 '15 at 14:57

1 Answers1

1
return num = num.substring(1).replace(/,/g , " ").replace(".", ",") + " $";

Use this to English formatted num.

sdn342323
  • 399
  • 3
  • 5
  • the substring is not working i used num.toFixed(2).replace(/,/g, " ").replace(".", ",") + " $"; its working to an extend: for example: using the regular expression 125900 is coming as 125900,00 $ but i want something like 125 900,00 $ – Softwarex3 Jun 15 '15 at 16:14