Skip to content

Instantly share code, notes, and snippets.

@michaelaguiar
Created June 12, 2015 23:02
Show Gist options
  • Save michaelaguiar/e5e39f3eb76d86558906 to your computer and use it in GitHub Desktop.
Save michaelaguiar/e5e39f3eb76d86558906 to your computer and use it in GitHub Desktop.
Abbreviate number. i.e. 250000 = 250K
function abbrNum(number, decPlaces) {
decPlaces = Math.pow(10,decPlaces);
var abbrev = ['K', 'M', 'B', 'T'];
for (var i = abbrev.length - 1; i >= 0; i--) {
var size = Math.pow(10,(i+1)*3);
if (size <= number) {
number = Math.round(number * decPlaces / size) / decPlaces;
if ((number == 1000) && (i < abbrev.length - 1)) {
number = 1;
i++;
}
number += abbrev[i];
break;
}
}
return number;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment