Skip to content

Instantly share code, notes, and snippets.

@motia
Created March 9, 2020 23:04
Show Gist options
  • Save motia/282878d4b75b3845f481477a3bc21aa3 to your computer and use it in GitHub Desktop.
Save motia/282878d4b75b3845f481477a3bc21aa3 to your computer and use it in GitHub Desktop.
function round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
var num = (Math.round(value * multiplier) / multiplier).toString();
return num.indexOf('.') ? num : num + '.0';
}
function shortNumberFormatter(number) {
if (number < 900) {
return number;
}
var chars = ['', 'k', 'm', 'b', 't'];
for (var i = 0; i < chars.length; i++) {
var temp = number / Math.pow(10, (i) * 3);
if (temp > 90) {
continue;
}
return round(temp, 1) + chars[i];
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment