Created
October 22, 2017 09:38
-
-
Save tex2e/f67f6a45b0ba0abd7b3586f8206c2eef to your computer and use it in GitHub Desktop.
turns 9999999 into 9,999,999
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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