Back to Snippets

Format Currency

typescript
Utilities

Format numbers as currency with proper locale support.

Code

1function formatCurrency(
2amount: number,
3currency: string = "USD",
4locale: string = "en-US"
5): string {
6return new Intl.NumberFormat(locale, {
7style: "currency",
8currency: currency,
9}).format(amount);
10}
11
12// Usage examples
13console.log(formatCurrency(1234.56));           // "$1,234.56"
14console.log(formatCurrency(1234.56, "EUR", "de-DE")); // "1.234,56 €"
15console.log(formatCurrency(1234.56, "GBP", "en-GB")); // "£1,234.56"
16console.log(formatCurrency(1234.56, "JPY", "ja-JP")); // "¥1,235"

Details

Language

typescript

Category

Utilities