Back to Snippets
CODE

Format Number as Currency

Description

Formats numbers as currency strings with locale support (no external libraries)

Code

/**
 * Format number as currency string
 * @param amount Numeric amount
 * @param currency Currency code (default: 'USD')
 * @param locale Locale (default: 'en-US')
 * @returns Formatted currency string
 */
function formatCurrency(
  amount: number,
  currency = 'USD',
  locale = 'en-US'
): string {
  return new Intl.NumberFormat(locale, {
    style: 'currency',
    currency
  }).format(amount);
}

// Usage Example
console.log(formatCurrency(1234.56)); // $1,234.56
console.log(formatCurrency(1234.56, 'EUR', 'de-DE')); // 1.234,56 €
console.log(formatCurrency(1234.56, 'JPY', 'ja-JP')); // ¥1,235

Usage

Perfect for e-commerce applications, financial dashboards, or any app requiring localized currency formatting

Tags

Number FormattingCurrencyInternationalization