QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Format Currency

Verified

Formats a number as localized currency using Intl.NumberFormat.

  • number
  • currency

Install

npx shadcn@latest add https://quicksnip.dev/r/format-currency.json

Code

/**
 * Formats a number as currency using the platform's Intl API.
 * Defaults to the runtime locale; pass `locale` for deterministic server output.
 */
export function formatCurrency(
  amount: number,
  currency = "USD",
  locale?: string,
  options: Intl.NumberFormatOptions = {},
): string {
  return new Intl.NumberFormat(locale, { style: "currency", currency, ...options }).format(amount);
}

Usage

formatCurrency(1234.5, "USD", "en-US"); // "$1,234.50"
formatCurrency(1234.5, "EUR", "de-DE"); // "1.234,50 €"