Skip to content

CompactNumber

MD · 1950B gzip budget

A large number shortened the way the reader's locale shortens it — 1.2K, 1.2万, 12 lakh.

Category
Internationalisation
Budget
md, several states or a live subscription
External runtime
React / React DOM only
axe fixtureSSR rendersource scanbundle budget

Preview

Live fixture
Preview locale
1,234,567

Try the component here. Locale-aware formatting and direction follow the selected language; example content and labels are not automatically translated. Overlay demos use interactive launchers.

Why this exists

CompactNumber

हिन्दी

A K/M/B ladder is not how most of the world shortens numbers. India groups by lakh and crore; Japan and China group by ten-thousands.

Formatting shortcut

1.2M

`${(n / 1e6).toFixed(1)}M`

CompactNumber

12.3 लाख

compact-number

Install

npx shadcn@latest add https://gear5-ui.vercel.app/r/compact-number.json

Copies the source into your project. Pulls in 3 shared registry items: format, use-locale, locale.

New here? Set up Tailwind and import aliases first →

Usage

import { LocaleProvider } from "@/lib/gear5/use-locale";
import { CompactNumber } from "@/components/gear5/compact-number";

export default function Visitors() {
  return (
    <LocaleProvider locale="ar-EG">
      <p lang="ar" dir="rtl">
        <CompactNumber value={1234567} /> زائر
      </p>
    </LocaleProvider>
  );
}

Source & props

Edit on GitHub ↗

Prop types and inline documentation are included below. This is repository source; the installer rewrites shared imports for your project.

"use client";

import { numberFormat } from "../lib/format";
import { useLocale } from "../lib/use-locale";

export interface CompactNumberProps {
  value: number;
  /**
   * "short" gives 1.2K, "long" gives 1.2 thousand. Both are locale-specific:
   * the same number is 1.2万 in Japanese, which groups by ten-thousands, and
   * 12 lakh in Indian English, which groups by lakhs and crores.
   */
  notation?: "short" | "long";
  /** Announce the exact value to assistive tech alongside the rounded one. */
  exactLabel?: boolean;
}

/*
 * Rendered inside a `suppressHydrationWarning` span.
 *
 * `Intl` output is not byte-identical across ICU versions, and Node's ICU is
 * not the browser's. This exact call produces "Jan 1 <U+2009>–<U+2009> 5, 2026"
 * on Node and "Jan 1 <U+0020>–<U+0020> 5, 2026" in Chrome — visually
 * identical, different bytes — so every server-rendered use would throw a
 * hydration error in a consumer's app through no fault of theirs.
 *
 * This is the case React documents the escape hatch for. The suppression is
 * scoped to this one text node, so a genuine structural mismatch anywhere else
 * still reports normally.
 */

/**
 * A large number shortened the way the reader's locale actually shortens it.
 *
 * Hand-rolled `n > 1000 ? (n / 1000) + "K"` is wrong for most of the world:
 * East Asian locales group by 10,000, and Indian locales by lakh and crore, so
 * a "K/M/B" ladder produces numbers a reader has to mentally convert.
 *
 * The rounded text is what sighted readers see; the exact value goes to screen
 * readers through the title/aria pair, because "1.2K followers" is a summary
 * and "1,234 followers" is the fact.
 */
export function CompactNumber({ value, notation = "short", exactLabel = true }: CompactNumberProps) {
  const { locale } = useLocale();

  const compact = numberFormat(locale, {
    notation: "compact",
    compactDisplay: notation,
    maximumFractionDigits: 1,
  }).format(value);

  const exact = numberFormat(locale).format(value);

  if (!exactLabel || compact === exact) {
    return <span suppressHydrationWarning>{compact}</span>;
  }

  return (
    <span title={exact} suppressHydrationWarning>
      <span aria-hidden="true">{compact}</span>
      <span className="sr-only">{exact}</span>
    </span>
  );
}

What CI checks

Quality contract
  • Bundled, minified and gzipped against its tier budget
  • Audited by axe in the state previewed above
  • Rendered through react-dom/server with no browser globals
  • Scanned for network calls, dangerous sinks, and unguarded animation