Skip to content

Instantly share code, notes, and snippets.

@ziad-saab
Created July 8, 2014 19:55
Show Gist options
  • Save ziad-saab/917bdc18252c18d6be7a to your computer and use it in GitHub Desktop.
Save ziad-saab/917bdc18252c18d6be7a to your computer and use it in GitHub Desktop.
Convert a number to a short version with K, M, B
// 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