0

Is there a way to use the below JavaScript code to use comma's? For example the var num = 1924.00 Is there way to get it to display as 1,924.00?

var num = parseFloat(totalAmount).toFixed(2);
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Steve Wolfe
  • 71
  • 1
  • 1
  • 10
  • You can do it like this `console.log(new Intl.NumberFormat('de-DE').format(yourNumber))` its already built in your browser – bill.gates Jan 24 '20 at 20:20

1 Answers1

0

You could use a specific regex like this one in the function below:

function format_currency( value ) {
    return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

If you want to use it in conjuction with the .toFixed(), just do like this:

format_currency( 1245.3.toFixed(2) );

Hope it helps.

Jazzpaths
  • 645
  • 5
  • 9