Created
July 8, 2014 19:55
-
-
Save ziad-saab/917bdc18252c18d6be7a to your computer and use it in GitHub Desktop.
Convert a number to a short version with K, M, B
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
// Taken from https://github.com/broofa/jslitmus and prettified | |
function numberToShortLabel(n) { | |
if (n == Infinity) { | |
return 'Infinity'; | |
} | |
else if (n > 1e9) { | |
n = Math.round(n/1e8); | |
return n/10 + 'B'; | |
} | |
else if (n > 1e6) { | |
n = Math.round(n/1e5); | |
return n/10 + 'M'; | |
} | |
else if (n > 1e3) { | |
n = Math.round(n/1e2); | |
return n/10 + 'K'; | |
} | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment