formatCurrency.ts
472 bytes
| 1 | import i18n from '@/i18n' |
|---|---|
| 2 | |
| 3 | export function formatCurrency( |
| 4 | amount: number, |
| 5 | symbol?: string | null, |
| 6 | decimals: number = 2, |
| 7 | ): string { |
| 8 | const locale = i18n.global.locale.value === 'et' ? 'et-EE' : 'en-US' |
| 9 | const abs = Math.abs(amount) |
| 10 | const formatted = abs.toLocaleString(locale, { |
| 11 | minimumFractionDigits: decimals, |
| 12 | maximumFractionDigits: decimals, |
| 13 | }) |
| 14 | const sign = amount < 0 ? '-' : '' |
| 15 | const sym = symbol ?? '' |
| 16 | return `${sign}${sym}${formatted}` |
| 17 | } |
| 18 | |