Created
December 5, 2012 15:49
-
-
Save tsileo/4216758 to your computer and use it in GitHub Desktop.
Convert bytes to human readble format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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