formatBytes
Commons
stable
Convert a raw byte count to a human-readable string (e.g. "1.5 MB"). Uses base-1024 with SI suffixes (B, KB, MB, GB, TB, PB). Non-finite or negative values return "0 B".
API
Signature
Full TypeScript signature sourced from the util file.
API
Parameters
Input parameters accepted by this util.
- bytesrequirednumber
- Raw byte count. Non-finite or negative values are treated as 0.
- decimalsnumber1
- Number of decimal places to display when the value is ≥ 1 KB. Defaults to 1.
API
Return value
The value returned by this util.
- Human-readable size string, e.g. "0 B", "512 B", "1.5 KB", "3.2 MB".
Usage
Examples
Ready-to-paste usage snippets.
Format file sizes