Skip to content

Instantly share code, notes, and snippets.

@robert-moore
Created April 8, 2016 12:09
Show Gist options
  • Save robert-moore/32149575e79ebf6d742f987e17d9784c to your computer and use it in GitHub Desktop.
Save robert-moore/32149575e79ebf6d742f987e17d9784c to your computer and use it in GitHub Desktop.
function numberFormat(amount, prefix, suffixArray, maxWholeDigitLength, decimalLength) {
var suffixIndex = 0;
while(amount > Math.pow(10, maxWholeDigitLength) && suffixIndex < suffixArray.length) {
amount /= 1000;
suffixIndex++;
}
return prefix + amount.toFixed(decimalLength) + suffixArray[suffixIndex];
};
var dollarSuffix = ["", "K", "M", "B"];
var dollarPrefix = "$";
numberFormat(1000000, dollarPrefix, dollarSuffix, 2, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment