Jeff
Jeff

Reputation: 197

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?

Upvotes: 13

Views: 19744

Answers (2)

Maksim Shamihulau
Maksim Shamihulau

Reputation: 1708

To display price/currency without decimal point or fraction digits, use Intl.NumberFormat with a few parameters maximumFractionDigits and minimumFractionDigits set to 0.

An example:

new Intl.NumberFormat(undefined, { 
  style: 'currency', 
  currency: 'USD',
  maximumFractionDigits: 0, 
  minimumFractionDigits: 0, 
}).format(1050.55)

Upvotes: 42

juvian
juvian

Reputation: 16068

Try this:

console.log(numberWithCommas(2145523));

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

Upvotes: 6

Related Questions