Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yairEO/33a682c0d8a35be25685 to your computer and use it in GitHub Desktop.
Save yairEO/33a682c0d8a35be25685 to your computer and use it in GitHub Desktop.
abbreviateNumber()
function abbreviateNumber(value){
var newValue = value,
suffixes, suffixNum, shortValue, precision, dotLessShortValue;
if (value >= 1000) {
suffixes = ["", "k", "m", "b","t"];
suffixNum = Math.floor( (""+Math.round(value)).length/3 );
shortValue = '';
for (precision = 2; precision >= 1; precision--) {
shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
if (dotLessShortValue.length <= 2)
break;
}
if (shortValue % 1 != 0)
shortNum = shortValue.toFixed(1);
newValue = shortValue+suffixes[suffixNum];
}
return newValue;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment