Created
September 28, 2011 20:54
-
-
Save mbklein/1249232 to your computer and use it in GitHub Desktop.
Convert number of bytes to human-readable string, reducing order of magnitude
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
BYTE_ORDERS = ['B','KB','MB','GB','TB','PB','EB'] | |
class Numeric | |
def bytestring | |
order = 0 | |
while (self > 1024 ** (order+1)) | |
order += 1 | |
end | |
"%.1f %s" % [self.to_f / (1024 ** order), BYTE_ORDERS[order]] | |
end | |
end |
Author
mbklein
commented
Sep 28, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment