Last active
January 1, 2016 20:59
-
-
Save iamjono/8200672 to your computer and use it in GitHub Desktop.
human_filesize returns a human readable file size from a supplied integer (bytes)
This file contains 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
[ | |
define human_filesize(size::integer) => { | |
/* ================================================================= | |
human_filesize returns a human readable file size from a supplied integer (bytes) | |
================================================================= */ | |
local( | |
displaysize = string, | |
calc = decimal, | |
kb = 1024, | |
mb = 1024 * #kb, | |
gb = 1024 * #mb, | |
tb = 1024 * #gb | |
) | |
#size < #kb ? return #size + ' bytes' | |
if(#size < #mb) => { | |
#calc = #size->asDecimal / #kb | |
return #calc->asString(-precision=0,-GroupChar=',') + ' kB' | |
else(#size < #gb) | |
#calc = #size->asDecimal / #mb | |
return #calc->asString(-precision=0,-GroupChar=',') + ' MB' | |
else(#size < #tb) | |
#calc = #size->asDecimal / #gb | |
return #calc->asString(-precision=0,-GroupChar=',') + ' GB' | |
else | |
#calc = #size->asDecimal / #tb | |
return #calc->asString(-precision=0,-GroupChar=',') + ' TB' | |
} | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment