How does pretty-bytes format byte counts into a human-readable string?
How does pretty-bytes format byte counts into a human-readable string?
About sindresorhus/pretty-bytes · 2 messages · last activity 6/8/2026
Based on the README, pretty-bytes converts byte counts to human-readable strings using a base-10 (SI Prefix) system by default.
Core Formatting Approach
The library takes a number and formats it with appropriate unit suffixes. For example [from the README]:
1337→'1.34 kB'100→'100 B'
Key Features
Default behavior (base-10): The library uses kilobytes, megabytes, etc., not kibibytes. This is base-10 formatting where 1 kB = 1,000 bytes.
Optional formatting modes (via the options parameter):
-
bitsoption – Format as bits instead of bytes:prettyBytes(1337, {bits: true})→'1.34 kbit'
-
binaryoption – Use binary prefixes (powers of 1024) instead:prettyBytes(1024, {binary: true})→'1 KiB'
-
signedoption – Include+for positive numbers:prettyBytes(42, {signed: true})→'+42 B'
-
localeoption – Localize decimal/thousand separators:prettyBytes(1337, {locale: 'de'})→'1,34 kB'(German uses comma)
-
fixedWidthoption – Pad output for alignment:prettyBytes(1337, {fixedWidth: 8})→' 1.34 kB'(with leading space)
The actual implementation details (which unit threshold triggers which suffix) aren't visible in the README excerpt. To see the exact formatting logic and thresholds, you'd need to examine the main source file.
FOLLOW_UPS:
- What are the exact byte thresholds for each unit?
- How does the library handle rounding and decimal places?
- Does pretty-bytes support custom unit definitions?
Want to ask your own question?
Open chat for sindresorhus/pretty-bytes →Public Q&A. Generated by RepoPilot from the actual source of sindresorhus/pretty-bytes. AI answers can be incomplete or stale — verify before relying on them.