Skip to content

Instantly share code, notes, and snippets.

@tsileo
Created December 5, 2012 15:49
Show Gist options
  • Save tsileo/4216758 to your computer and use it in GitHub Desktop.
Save tsileo/4216758 to your computer and use it in GitHub Desktop.
Convert bytes to human readble format
var convert_bytes = function(bytes, force_unit) {
var return_val = "";
$.each(['bytes','KB','MB','GB'], function(index, unit) {
if (force_unit == unit) {
new_size = Math.round(bytes*100)/100;
return [new_size, unit];
}
if (bytes < 1024.0 && force_unit != "TB") {
return_val = [Math.round(bytes*100)/100, unit];
}
bytes /= 1024.0;
})
if (!return_val) {
new_size = Math.round(bytes*100)/100;
return_val = [new_size, "TB"];
}
return return_val
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment