Skip to content

Instantly share code, notes, and snippets.

@ophentis
Created March 2, 2013 16:36
Show Gist options
  • Save ophentis/5071835 to your computer and use it in GitHub Desktop.
Save ophentis/5071835 to your computer and use it in GitHub Desktop.
function to output bytes with greatest unit
var units = ' KMGTPEZYXWVU';
function toHumanSize(bytes) {
if (bytes <= 0) { return 0; }
var t2 = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), 12);
return (Math.round(bytes * 100 / Math.pow(1024, t2)) / 100) + units.charAt(t2).replace(' ', '') + 'B';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment