Skip to content

Instantly share code, notes, and snippets.

@tex2e
Created October 22, 2017 09:38
Show Gist options
  • Save tex2e/f67f6a45b0ba0abd7b3586f8206c2eef to your computer and use it in GitHub Desktop.
Save tex2e/f67f6a45b0ba0abd7b3586f8206c2eef to your computer and use it in GitHub Desktop.
turns 9999999 into 9,999,999
// turns 9999999 into 9,999,999
function beautify(num) {
var str = '';
if (!isFinite(num)) return 'Infinity';
if (num.toString().indexOf('e') != -1) return num.toString();
num = Math.round(num * 10000000) / 10000000; // get rid of weird rounding errors
num = Math.floor(num);
num = (num + '').split('').reverse();
for (var i in num) {
if (i % 3 == 0 && i > 0) str = ',' + str;
str = num[i] + str;
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment