Skip to content

Latest commit

 

History

History
17 lines (15 loc) · 402 Bytes

convert-bytes-to-human-readable-format.md

File metadata and controls

17 lines (15 loc) · 402 Bytes
title description author tags
Convert Bytes to Human-Readable Format
Converts a size in bytes to a human-readable format.
axorax
bytes,format
def bytes_to_human_readable(num):
    for unit in ['B', 'KB', 'MB', 'GB', 'TB', 'PB']:
        if num < 1024:
            return f"{num:.2f} {unit}"
        num /= 1024

# Usage:
bytes_to_human_readable(123456789) # Returns: '117.74 MB'