Created
December 13, 2024 12:55
-
-
Save orion55/fd42abbc2d34f8b6735ef417fa0cd5c4 to your computer and use it in GitHub Desktop.
formatCurrency
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const LOCALE_MAP: Record<string, string> = { | |
| RUB: 'ru-RU', | |
| USD: 'en-US', | |
| EUR: 'de-DE', | |
| }; | |
| export function formatCurrency(amount: number, currencyCode: string): string { | |
| const locale = LOCALE_MAP[currencyCode] || 'ru-RU'; | |
| const formattedAmount = new Intl.NumberFormat(locale, { | |
| style: 'currency', | |
| currency: currencyCode, | |
| minimumFractionDigits: 0, | |
| maximumFractionDigits: 2, | |
| }).format(amount); | |
| return formattedAmount.endsWith(',00') || formattedAmount.endsWith('.00') | |
| ? formattedAmount.slice(0, -3) | |
| : formattedAmount; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment