Skip to content
Design System
GitHub

Format number

v0.64.0, changelog

The kit's one number path: formatCount (135,287, or compact 135K only where space is short), formatNumber for other numbers and resolveLocale, in English by default so server and browser match.

Install
npx shadcn add @syncrobotic/format-number

Overview

When to use

  • A count in a table, a header, a detail page or a figure: formatCount(n, { locale }).
  • A count on a tab, a chart's ticks or a compact chip: formatCount(n, { compact: true }), with the full number one step away.
  • A reading, a share, a price: formatNumber(n, { locale, maximumFractionDigits: 1 }) (any Intl.NumberFormat option).

Use something else

Usage rules

  • Every number the screen writes goes through it: formatCount(n, { locale }) for a count (whole things: rows, robots, matches), formatNumber(n, { locale, ...options }) for any other number (a reading, a share, a price).
  • A count is in full with grouping (135,287) in tables, headers, detail pages and figures; compact (135K; 14萬 in zh-TW) only on a tab's count, a chart's ticks or a compact chip, with the full number one step away.
  • English without a locale, and for a tag Intl cannot read ("en_US"), so the server and the browser write the same digits. Never toLocaleString(): it follows the runtime's language (lint:numbers bans it in the kit).
  • Write the result in tabular-nums.

API

What the file exports. The source is in the Source sheet above.

resolveLocale(locale?: string): string
The locale numbers are written in: "en" without one (never the runtime's default, so server and browser agree), and "en" for a tag Intl cannot read ("en_US", ""), so a bad setting writes English rather than break the component.
numberFormat(locale: string | undefined, options: Intl.NumberFormatOptions = {}): Intl.NumberFormat
A cached Intl.NumberFormat for `locale` (resolved as `resolveLocale` does) and `options`.
formatNumber(n: number, { locale, ...options }: Intl.NumberFormatOptions & { locale?: string } = {}): string
A number that is not a count, as the kit writes it.
formatCount(n: number, { compact = false, locale }: { compact?: boolean; locale?: string } = {})
A count through Intl.NumberFormat in `locale` (English when none is given).
plural(n: number, one: string, many: string, { locale }: { locale?: string } = {})
A count and its noun, the count as formatCount writes it: plural(3, "line", "lines") is "3 lines", 1200 "1,200 lines".

Changelog

  • 0.64.02026-12-01Formatted with Prettier (class order sorted); the code is unchanged.
  • 0.58.02026-11-28Stable: shipped ten releases with no breaking change, with unit tests, measured accessibility (components) and use/instead docs. Its API now changes only after a release deprecates what goes, with a migration line.
  • 0.53.02026-11-27Its doc opens with its description, word for word; the code is unchanged.

How to upgrade