/** * Toolkit - Currency Formatting * * Simple currency formatting. Currency code comes from user's WHMCS profile. * Typically JPY for this application. */ export type SupportedCurrency = "JPY" | "USD" | "EUR"; /** * Format a number as currency using WHMCS currency data * * @param amount - The numeric amount to format * @param currencyCode - Currency code from WHMCS API (e.g., "JPY", "USD", "EUR") * @param currencyPrefix - Currency symbol from WHMCS API (e.g., "¥", "$", "€") * * @example * formatCurrency(1000, "JPY", "¥") // ¥1,000 * formatCurrency(1000, "USD", "$") // $1,000.00 * formatCurrency(1000, "EUR", "€") // €1,000.00 */ export function formatCurrency( amount: number, currencyCode: string, currencyPrefix: string ): string { // Determine fraction digits based on currency const fractionDigits = currencyCode === "JPY" ? 0 : 2; // Format the number with appropriate decimal places const formattedAmount = amount.toLocaleString("en-US", { minimumFractionDigits: fractionDigits, maximumFractionDigits: fractionDigits, }); // Add currency prefix return `${currencyPrefix}${formattedAmount}`; } /** * Parse a currency string to a number */ export function parseCurrency(value: string): number | null { // Remove currency symbols, commas, and whitespace const cleaned = value.replace(/[¥$€,\s]/g, ""); const parsed = Number.parseFloat(cleaned); return Number.isFinite(parsed) ? parsed : null; }