RepoPilot

Can I get binary (KiB, MiB) units instead of decimal? How?

About sindresorhus/pretty-bytes · 2 messages · last activity 7/13/2026

Question

Can I get binary (KiB, MiB) units instead of decimal? How?

RepoPilot

Yes, you can use binary units by setting the binary option to true.

The library supports binary prefixes (KiB, MiB, GiB, etc.) through the binary option. When enabled, it uses base-1024 instead of base-1000 for calculations [index.js:L139].

Example:

import prettyBytes from 'pretty-bytes';

prettyBytes(1024, {binary: true});
//=> '1 KiB'

How it works:

  • With binary: false (default): Uses decimal units like kB, MB, GB (base-1000)
  • With binary: true: Uses binary units like KiB, MiB, GiB (base-1024)

The library switches between two unit arrays:

  • BIBYTE_UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'] [index.js:L13-L21]
  • BYTE_UNITS = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] (default) [index.js:L1-L9]

The selection happens at [index.js:L139-L140], where it picks the appropriate unit array based on the binary option.

FOLLOW_UPS:

  • Can I combine binary with bits option?
  • What's the difference between KiB and kB?
  • How does binary option affect calculations?

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.