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
i = 0
while size >= 1024 and i < len(units) - 1:
    size /= 1024
    i += 1
return f'{size:.2f} {units[i]}'

Parameters

Size in bytes.

Server

More Python Snippets