Bytes to Human Readable

Convert bytes to human readable format (KB, MB, GB, etc.) for file sizes.

Code

Utilities
units = ['B', 'KB', 'MB', 'GB', 'TB']
i = 0
size = bytes.to_f
while size >= 1024 && i < units.length - 1
  size /= 1024
  i += 1
end
return "#{'%.2f' % size} #{units[i]}"

Parameters

Number of bytes.

Server

More Ruby Snippets