Created
June 20, 2012 18:53
-
-
Save ik5/0e10b413c6d9ae4dc712 to your computer and use it in GitHub Desktop.
human readable sizes
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
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