1function formatNumber(num) {
2 return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
3}
4
5console.info(formatNumber(2665)) // 2,665
6console.info(formatNumber(102665)) // 102,665
7console.info(formatNumber(111102665)) // 111,102,665
8console.info(formatNumber(1240.5)) // 1,240.5
9console.info(formatNumber(1000240.5)) // 1,000,240.5
1// Create our number formatter.
2var formatter = new Intl.NumberFormat('en-US', {
3 style: 'currency',
4 currency: 'USD',
5});
6
7formatter.format(2500); /* $2,500.00 */
1var x = parseFloat('9.656');
2
3x.toFixed(0); // returns 10
4x.toFixed(2); // returns 9.66
5x.toFixed(4); // returns 9.6560
6x.toFixed(6);
1function numberWithCommas(x) {
2 return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
3}
4
1function m(n,d){x=(''+n).length,p=Math.pow,d=p(10,d)
2x-=x%3
3return Math.round(n*d/p(10,x))/d+" kMGTPE"[x/3]}