Skip to content

Instantly share code, notes, and snippets.

@ik5
Created June 20, 2012 18:53
Show Gist options
  • Save ik5/0e10b413c6d9ae4dc712 to your computer and use it in GitHub Desktop.
Save ik5/0e10b413c6d9ae4dc712 to your computer and use it in GitHub Desktop.
human readable sizes
def human_size(size, si = false)
return '0' if size == 0
base = si ? 1000 : 1024
bytes = %w(B KB MB GB TB PB EB ZB YB)
cnt = Math.log(size, base).floor
size = size / (base * 1.0) ** cnt
fmt = size < 10 ? '%.1f %s' : '%i %s'
sprintf(fmt, size, bytes[cnt])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment