Last active
December 22, 2015 01:09
-
-
Save jeroentbt/6394300 to your computer and use it in GitHub Desktop.
snippet: JS: Humanize bytes to KB / MB / GB
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
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