Skip to content

formatSize

Format a byte count into a human-readable string with the appropriate unit.

Each unit is 1024 of the previous (binary prefix). The result is formatted with one decimal place.

Available since v2.0.0

import { formatSize } from '@helpers4/number';
formatSize(bytes: number): string
ParameterTypeDescription
bytesnumberA non-negative integer representing a byte count.

string — A human-readable string such as '0.0B', '1.5KB', '3.2MB'.

Converts a raw byte count to a human-readable string using binary prefixes.

formatSize(0)             // '0.0B'
formatSize(512)           // '512.0B'
formatSize(1024)          // '1.0KB'
formatSize(1_048_576)     // '1.0MB'
formatSize(1_073_741_824) // '1.0GB'

View source on GitHub