Skip to content
Protocore Design Systemv1.6.9

/// Data Display

FormatByte

A deterministic file-size renderer — 1536 becomes 1.5 KB, in mono tabular figures.

import { FormatByte } from "@protocore/pds";
View as Markdown

Basics

Pass a byte count as value. It scales to the largest whole unit and renders mono tabular figures, so a column of sizes stays aligned. Sub-kilobyte values stay in whole bytes — you never see a fractional B.

512 B1.5 KB2.4 MB5.4 GB

Binary units and precision

binary switches to 1024-based IEC units (KiB, MiB, GiB) for the cases where that distinction matters — disk usage, memory. decimals sets the fractional precision on the scaled value.

1.0 MB1.0 MiB1.54 MB2 MB

The pure formatter

The math lives in an exported formatBytes(bytes, options) function — call it directly when you need the string outside JSX (a table cell value, a tooltip, an aria-label). Both the function and the component take an explicit locale (default "en") so a server render and its client hydration produce identical output.

A non-finite input renders the literal value with a data-invalid marker rather than fabricating an authoritative-looking size.

Usage

Do

  • Pass a raw byte count and let the component pick the unit.
  • Use `binary` for disk, memory, and anything the OS reports in KiB/MiB.
  • Call `formatBytes` directly for strings outside JSX.
  • Keep a fixed `locale` so SSR and client agree.

Don't

  • Pre-divide the value; pass whole bytes.
  • Mix binary and decimal units in one column; pick one basis.
  • Expect fractional bytes; sub-KB values render as whole `B`.

Accessibility

  • Renders a plain `<span>`; the formatted string is the accessible text.
  • Mono tabular figures keep a column of sizes vertically aligned.

FormatByte props

PropTypeDefaultDescription
binarybooleanfalseUse 1024-based (IEC: KiB/MiB) units instead of 1000-based (KB/MB).
classNamestring
decimalsnumber1Fractional digits on the scaled value.
localestring"en"BCP-47 locale for the decimal separator. Fixed so SSR and hydration agree.
styleCSSProperties
value *numberThe size in bytes to format.

Related

  • NumberFormatterA thin Intl.NumberFormat wrapper for decimals, currency, percent, and units, in mono tabular figures.
  • MoneyAmountA deterministic currency renderer — mono tabular figures, minor-unit aware, compact and sign-colored.
  • RelativeTimeAn auto-updating "3m ago" timestamp on a real <time> element, with an absolute UTC tooltip.