//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"
formatedNumber = new Intl.NumberFormat().format(2561556862056.12)
console.log(formatedNumber) // "2,561,556,862,056.12"
foramtNumber = (num,div=",")=>{
return num.toString().replace(/B(?=(d{3})+(?!d))/g, div);
}
x.toString().toLocaleString(); // x must be a number