Format Bytes (IEC)

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

Code

Utilities
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
let i = 0, size = bytes;
while (size >= 1024 && i < units.length - 1) { size /= 1024; i++; }
return `${size.toFixed(2)} ${units[i]}`;

Parameters

Size in bytes.

Browser·fetch() may be limited by CORS

More JavaScript Snippets