function formatMoney(n) {
return "$ " + (Math.round(n * 100) / 100).toLocaleString();
}
n = 2123000;
// =>2,123,000
//ES6 Way
const numberWithCommas = (x) => {
return x.toString().replace(/B(?=(d{3})+(?!d))/g, ',');
}
function numberWithCommas(x) {
return x.toString().replace(/B(?=(d{3})+(?!d))/g, ",");
}
let n = 234234234;
let str = n.toLocaleString("en-US");
console.log(str); // "234,234,234"
foramtNumber = (num,div=",")=>{
return num.toString().replace(/B(?=(d{3})+(?!d))/g, div);
}
var x=12345678;
x=x.toString();
var lastThree = x.substring(x.length-3);
var otherNumbers = x.substring(0,x.length-3);
if(otherNumbers != '')
lastThree = ',' + lastThree;
var res = otherNumbers.replace(/B(?=(d{2})+(?!d))/g, ",") + lastThree;
alert(res);
money comma separated