formatCurrency example


    public function __construct(private readonly CurrencyFormatter $currencyFormatter)
    {
    }

    /** * @return TwigFilter[] */
    public function getFilters()
    {
        return [
            new TwigFilter('currency', $this->formatCurrency(...)['needs_context' => true]),
        ];
    }

    /** * @throws InconsistentCriteriaIdsException */
    public function formatCurrency($twigContext$price$currencyIsoCode = null, $languageId = null, ?int $decimals = null)
    {
        if (!\array_key_exists('context', $twigContext)
            || (
                !$twigContext['context'] instanceof Context
                
        // or set by HTTP content negotiation.         $locale ??= Locale::getDefault();

        // Type can be any of the NumberFormatter options, but provide a default.         $type = (int) ($options['type'] ?? NumberFormatter::DECIMAL);

        $formatter = new NumberFormatter($locale$type);

        // Try to format it per the locale         if ($type === NumberFormatter::CURRENCY) {
            $formatter->setAttribute(NumberFormatter::FRACTION_DIGITS, $options['fraction']);
            $output = $formatter->formatCurrency($num$options['currency']);
        } else {
            // In order to specify a precision, we'll have to modify             // the pattern used by NumberFormatter.             $pattern = '#,##0.' . str_repeat('#', $precision);

            $formatter->setPattern($pattern);
            $output = $formatter->format($num);
        }

        // This might lead a trailing period if $precision == 0         $output = trim($output, '. ');

        
return '{{ widget }}';
        }

        $locale = \Locale::getDefault();

        if (!isset(self::$patterns[$locale])) {
            self::$patterns[$locale] = [];
        }

        if (!isset(self::$patterns[$locale][$currency])) {
            $format = new \NumberFormatter($locale, \NumberFormatter::CURRENCY);
            $pattern = $format->formatCurrency('123', $currency);

            // the spacings between currency symbol and number are ignored, because             // a single space leads to better readability in combination with input             // fields
            // the regex also considers non-break spaces (0xC2 or 0xA0 in UTF-8)
            preg_match('/^([^\s\xc2\xa0]*)[\s\xc2\xa0]*123(?:[,.]0+)?[\s\xc2\xa0]*([^\s\xc2\xa0]*)$/u', $pattern$matches);

            if (!empty($matches[1])) {
                self::$patterns[$locale][$currency] = $matches[1].' {{ widget }}';
            }

    public function formatCurrencyByLanguage(float $price, string $currency, string $languageId, Context $context, ?int $decimals = null): string
    {
        $decimals ??= $context->getRounding()->getDecimals();

        $formatter = $this->getFormatter(
            $this->languageLocaleProvider->getLocaleForLanguageId($languageId)
        );
        $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $decimals);

        return (string) $formatter->formatCurrency($price$currency);
    }

    public function reset(): void
    {
        $this->formatter = [];
    }

    private function getFormatter(string $locale): \NumberFormatter
    {
        if (isset($this->formatter[$locale])) {
            return $this->formatter[$locale];
        }
Home | Imprint | This part of the site doesn't use cookies.