Reputation: 197
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
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
Reputation: 16068
Try this:
console.log(numberWithCommas(2145523));
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
Upvotes: 6