Skip to content

Instantly share code, notes, and snippets.

@robertklep
Created October 6, 2012 09:19
Show Gist options
  • Save robertklep/3844473 to your computer and use it in GitHub Desktop.
Save robertklep/3844473 to your computer and use it in GitHub Desktop.
Create a humanized size string from a Javascript Number instance
Number.prototype.sizify = function(precision) {
if (precision === undefined)
precision = 1;
if (this > 1200000000)
return (this / 1000000000.0).toFixed(precision) + ' G';
if (this > 1200000)
return (this / 1000000.0).toFixed(precision) + ' M';
if (this > 1200)
return (this / 1000.0).toFixed(precision) + ' K';
return this.toFixed(precision);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment