12

I want to convert my JavaScript number into a currency number, but with any currency symbol

Suppose this is my number:

var number = 43434;

The result should be like this:

43,434

And not this:

$43,434
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ancient
  • 3,007
  • 14
  • 55
  • 104
  • 4
    Please show us what you've tried, how its not working, etc. This site isn't some place where you get people to do your work for you; if you expect time and effort to be put into answers, you have to put time and effort into your question. – Daedalus Jul 10 '13 at 06:21
  • Supporting @Daedalus and agree with Alex link, its awesome one. – Hariharan Jul 10 '13 at 06:23
  • There are plenty of similar questions. You can find how to do it here: http://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript and there http://stackoverflow.com/questions/13621769/adding-comma-as-thousands-separator-javascript-output-being-deleted-instead – Alex Jul 10 '13 at 06:25
  • One way is to use D3 library , see [this post](https://stackoverflow.com/a/51423760/7848529) – aabiro Jul 19 '18 at 13:45

1 Answers1

23

Using one regex /(\d)(?=(\d{3})+(?!\d))/g:

"1234255364".replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
"1,234,255,364"

To achieve this with an integer you can use +"" trick:

var number = 43434;
(number + "").replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); // 43,434
mishik
  • 9,973
  • 9
  • 45
  • 67
  • 2
    If anyone is getting ".replace is not defined" error trying to use it on a number then apply toString() before using replace, e.g. $number.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,") – Gogol Nov 20 '13 at 10:51