RepoPilot

How does pretty-bytes format byte counts into a human-readable string?

About sindresorhus/pretty-bytes · 2 messages · last activity 6/8/2026

Question

How does pretty-bytes format byte counts into a human-readable string?

RepoPilot

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):

  1. bits option – Format as bits instead of bytes:

    • prettyBytes(1337, {bits: true})'1.34 kbit'
  2. binary option – Use binary prefixes (powers of 1024) instead:

    • prettyBytes(1024, {binary: true})'1 KiB'
  3. signed option – Include + for positive numbers:

    • prettyBytes(42, {signed: true})'+42 B'
  4. locale option – Localize decimal/thousand separators:

    • prettyBytes(1337, {locale: 'de'})'1,34 kB' (German uses comma)
  5. fixedWidth option – 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.