formatNumber (utils/format.ts)

chanhdai.com · src/utils/format.ts · typescript · MIT
ui, tag:embed-classified

* Always `en-US`, never the runtime locale: a client component would pick up the visitor's locale while a server component picks up the host's, so the same figure could render as `1,234` in one place and `1.234` in another on the same page. Module-level because constructing an `I

// Source: https://github.com/ncdai/chanhdai.com | License: MIT | Copyright 2026 Chánh Đại
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Always `en-US`, never the runtime locale: a client component would pick up
 * the visitor's locale while a server component picks up the host's, so the
 * same figure could render as `1,234` in one place and `1.234` in another on
 * the same page.
 *
 * Module-level because constructing an `Intl` formatter is the expensive part,
 * not formatting with it.
 */
const NUMBER_FORMATTER = new Intl.NumberFormat("en-US")

const COMPACT_NUMBER_FORMATTER = new Intl.NumberFormat("en-US", {
  notation: "compact",
  compactDisplay: "short",
})

export function formatNumber(value: number): string {
  return NUMBER_FORMATTER.format(value)
}

e

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key