twigphp/Twig · error · Twig\Error\RuntimeError

Unable to format the given number as a currency.

Error message

Unable to format the given number as a currency.

What it means

IntlExtension::formatCurrency() wraps NumberFormatter::formatCurrency(); when that returns false — meaning ICU could not format the amount for the given currency/locale — the extension throws this RuntimeError instead of emitting 'false'.

Solutions

  1. Verify the ISO 4217 currency code (3 uppercase letters, real code like 'USD','EUR').
  2. Ensure $amount is an int/float; cast numeric strings with (float).
  3. Validate the value is finite (not NAN/INF) before formatting.
  4. Confirm the intl extension and ICU data are installed (php -m, php --ri intl).

Example fix

// before
{{ amount|format_currency(currencyCode) }}
// after
{% if currencyCode matches '/^[A-Z]{3}$/' %}{{ amount|format_currency(currencyCode) }}{% endif %}
Defensive patterns

Strategy: try-catch

Validate before calling

if (!preg_match('/^[A-Z]{3}$/', $currency) || !is_numeric($amount)) { throw new \InvalidArgumentException('Bad currency/amount'); }

Type guard

function isFormattableCurrency(mixed $amount, string $currency): bool { return is_int($amount)||is_float($amount) && is_finite($amount) && preg_match('/^[A-Z]{3}$/', $currency) === 1; }

Try / catch

try { $s = format_currency($amount, $currency); } catch (Twig\Error\RuntimeError $e) { $s = number_format((float)$amount, 2).' '.$currency; }

Prevention

When it happens

Trigger: Calling twig format_currency($amount, 'XXX') with an unknown/invalid currency code, or a non-numeric $amount (e.g. NaN, or a non-numeric string) with certain attrs, or 'intl' ICU failing on the locale.

Common situations: Typos or placeholder currency codes ('XXX', '') from config; passing null/NaN amounts; running without the intl extension properly configured (no ICU data for a locale).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of twigphp/Twig@a414c3a491 (2026-09-13). Data as JSON: /api/errors/4150d98106a518fd. Report an issue: GitHub.

Appendix: source

Thrown at extra/intl-extra/IntlExtension.php:378

            return [];
        }
    }

    public function getTimezoneNames(?string $locale = null): array
    {
        try {
            return Timezones::getNames($locale);
        } catch (MissingResourceException $exception) {
            return [];
        }
    }

    public function formatCurrency($amount, string $currency, array $attrs = [], ?string $locale = null): string
    {
        $formatter = $this->createNumberFormatter($locale, 'currency', $attrs);

        if (false === $ret = $formatter->formatCurrency($amount, $currency)) {
            throw new RuntimeError('Unable to format the given number as a currency.');
        }

        return $ret;
    }

    public function formatNumber($number, array $attrs = [], string $style = 'decimal', string $type = 'default', ?string $locale = null): string
    {
        if (!isset(self::NUMBER_TYPES[$type])) {
            throw new RuntimeError(\sprintf('The type "%s" does not exist, known types are: "%s".', $type, implode('", "', array_keys(self::NUMBER_TYPES))));
        }

        $formatter = $this->createNumberFormatter($locale, $style, $attrs);

        if (false === $ret = $formatter->format($number, self::NUMBER_TYPES[$type])) {
            throw new RuntimeError('Unable to format the given number.');
        }

        return $ret;

View on GitHub (pinned to a414c3a491)