Skip to content

Instantly share code, notes, and snippets.

@orion55
Created December 13, 2024 12:55
Show Gist options
  • Select an option

  • Save orion55/fd42abbc2d34f8b6735ef417fa0cd5c4 to your computer and use it in GitHub Desktop.

Select an option

Save orion55/fd42abbc2d34f8b6735ef417fa0cd5c4 to your computer and use it in GitHub Desktop.
formatCurrency
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