Format Bytes (IEC)

Format bytes using IEC units (KiB, MiB, GiB) based on powers of 1024.

Code

Utilities
units = ['B', 'KiB', 'MiB', 'GiB', 'TiB']
size = bytes.to_f
i = 0
while size >= 1024 && i < units.length - 1
  size /= 1024
  i += 1
end
return '%.2f %s' % [size, units[i]]

Parameters

Size in bytes.

Server

More Ruby Snippets