Skip to content

Instantly share code, notes, and snippets.

@iamjono
Last active January 1, 2016 20:59
Show Gist options
  • Save iamjono/8200672 to your computer and use it in GitHub Desktop.
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)
[
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