Created
October 7, 2015 23:10
-
-
Save yairEO/33a682c0d8a35be25685 to your computer and use it in GitHub Desktop.
abbreviateNumber()
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
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