Skip to content

Instantly share code, notes, and snippets.

@jeroentbt
Last active December 22, 2015 01:09
Show Gist options
  • Save jeroentbt/6394300 to your computer and use it in GitHub Desktop.
Save jeroentbt/6394300 to your computer and use it in GitHub Desktop.
snippet: JS: Humanize bytes to KB / MB / GB
humanize = function (size) {
var gb = Math.pow(1024, 3);
var mb = Math.pow(1024, 2);
var kb = 1024;
if (size >= gb)
return Math.floor(size / gb) + ' GB';
else if (size >= 1024^2)
return Math.floor(size / mb) + ' MB';
else if (size >= 1024)
return Math.floor(size / kb) + ' KB';
else
return size + ' Bytes';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment