Skip to content

Instantly share code, notes, and snippets.

@MeirKriheli
Created June 20, 2012 19:59
Show Gist options
  • Save MeirKriheli/2961852 to your computer and use it in GitHub Desktop.
Save MeirKriheli/2961852 to your computer and use it in GitHub Desktop.
def humanize_number(size)
return '0' if size == 0
bytes = %w(B KB MB GB TB PB EB ZB YB)
cnt = Math.log(size, 1024).floor
size = size / 1024.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