import type { Currency } from '../enums/currency'; import { LanguageUtils } from './language-utils'; export class CurrencyUtils { /** * Converts number to currency representation * * @param value Number * @param currency Currency symbol */ static floatToCurrency( value: number, currency: Currency | string | any, killZeroCents?: boolean, ): string { if (currency > -1 && currency < 1000) { if (currency == 978) { currency = 'EUR'; } else if (currency == 203) { currency = 'CZK'; } else if (currency == 826) { currency = 'GBP'; } else if (currency == 348) { currency = 'HUF'; } else if (currency == 985) { currency = 'PLN'; } else if (currency == 643) { currency = 'RUB'; } else if (currency == 840) { currency = 'USD'; } } return LanguageUtils.floatToCurrency( value, currency, killZeroCents, ); } static getCurrencySymbol(currency: Currency | string | any) { // Format a sample value, then strip the numeric part so only the currency symbol/code remains. // We strip ALL digits, decimal/grouping separators and surrounding whitespace. `\s` already // matches every space variant the locale formatter emits (regular space, NBSP U+00A0 and // narrow NBSP U+202F). Stripping the literal "1.11"/"1,11" sample alone is not enough: // zero-decimal currencies (e.g. HUF) round the sample to "1", which would otherwise leak that // digit into the result (producing "1 Ft" instead of "Ft"). return CurrencyUtils.floatToCurrency(1.11, currency) .replace(/[\d\s.,]/g, '') .trim(); } }