Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a JavaScript number to a currency format, but without "$" or any currency symbol [duplicate]

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
like image 246
Ancient Avatar asked Jul 10 '13 06:07

Ancient


1 Answers

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
like image 150
mishik Avatar answered Nov 09 '22 06:11

mishik