Format numbers with locale and options. Currency, decimals, compact.
export function formatNumber(
num: number,
options: Intl.NumberFormatOptions = {}
): string {
return new Intl.NumberFormat('en-US', {
maximumFractionDigits: 2,
...options,
}).format(num);
}
// formatNumber(1234.5) => "1,234.5"
// formatNumber(1234.5, { style: 'currency', currency: 'USD' }) => "$1,234.50"
// formatNumber(1200, { notation: 'compact' }) => "1.2K"
Uses Intl.NumberFormat. Pass locale and options (currency, compact, min/max fraction digits).
Display prices, counts, or compact notation (1.2K).
formatNumber(price, { style: 'currency', currency: 'USD' });