formatToCurrency function is a utility for formatting numeric values into currency strings with proper locale-specific formatting. It supports multiple international currencies and provides flexible options for displaying currency symbols.ts
import { formatToCurrency } from "@arkyn/shared";
value (required)numbercurrency (required)"USD", "EUR", "JPY", "GBP", "AUD", "CAD", "CHF", "CNY", "SEK", "NZD", "BRL", "INR", "RUB", "ZAR", "MXN", "SGD", "HKD", "NOK", "KRW", "TRY", "IDR", "THB"config (optional)objectconfig.showPrefix (optional)true, the currency symbol (e.g., "$", "R$", "€") is displayed. When set to false, only the numeric value with appropriate thousand separators and decimal points is returned.booleantruestringtypescript
import { formatToCurrency } from "@arkyn/shared";const formatted = formatToCurrency(1234.56, "USD", { showPrefix: true });console.log(formatted);// Output: "$1,234.56"
typescript
import { formatToCurrency } from "@arkyn/shared";try {const formatted = formatToCurrency(1234.56, "ABC");console.log(formatted);} catch (error) {console.error(error);// Output: Error: Unsupported currency code}
Intl.NumberFormat API internally, which ensures that currency formatting follows international standards and locale-specific conventions for each currency.removeCurrencySymbols utility when showPrefix is set to false, ensuring clean numeric output without any currency symbols or prefixes.