Skip to content

How to format Number to Currency without decimal point in JavaScript?

var computeST = 2145523
var resultFormat= <-- format the computeST here -->
alert(resultFormat);

the display alert should be

2,145,523

Anyone can help me?

Answer

Try this:

console.log(numberWithCommas(2145523));

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