1function formatMoney(n) {
2 return "$ " + (Math.round(n * 100) / 100).toLocaleString();
3}
4
5n = 2123000;
6// =>2,123,000
1function numberWithCommas(x) {
2 return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
3}
1function numberWithCommas(x) {
2 return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
3}
1var number = 3500;
2
3console.log(new Intl.NumberFormat().format(number));
4// → '3,500' if in US English locale
5