{"record":{"id":"4150d98106a518fd","repo":"twigphp/Twig","slug":"unable-to-format-the-given-number-as-a-currency","errorCode":null,"errorMessage":"Unable to format the given number as a currency.","messagePattern":"Unable to format the given number as a currency\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/intl-extra/IntlExtension.php","lineNumber":378,"sourceCode":"            return [];\n        }\n    }\n\n    public function getTimezoneNames(?string $locale = null): array\n    {\n        try {\n            return Timezones::getNames($locale);\n        } catch (MissingResourceException $exception) {\n            return [];\n        }\n    }\n\n    public function formatCurrency($amount, string $currency, array $attrs = [], ?string $locale = null): string\n    {\n        $formatter = $this->createNumberFormatter($locale, 'currency', $attrs);\n\n        if (false === $ret = $formatter->formatCurrency($amount, $currency)) {\n            throw new RuntimeError('Unable to format the given number as a currency.');\n        }\n\n        return $ret;\n    }\n\n    public function formatNumber($number, array $attrs = [], string $style = 'decimal', string $type = 'default', ?string $locale = null): string\n    {\n        if (!isset(self::NUMBER_TYPES[$type])) {\n            throw new RuntimeError(\\sprintf('The type \"%s\" does not exist, known types are: \"%s\".', $type, implode('\", \"', array_keys(self::NUMBER_TYPES))));\n        }\n\n        $formatter = $this->createNumberFormatter($locale, $style, $attrs);\n\n        if (false === $ret = $formatter->format($number, self::NUMBER_TYPES[$type])) {\n            throw new RuntimeError('Unable to format the given number.');\n        }\n\n        return $ret;","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/intl-extra/IntlExtension.php#L360-L396","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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).","solutions":["Verify the ISO 4217 currency code (3 uppercase letters, real code like 'USD','EUR').","Ensure $amount is an int/float; cast numeric strings with (float).","Validate the value is finite (not NAN/INF) before formatting.","Confirm the intl extension and ICU data are installed (php -m, php --ri intl)."],"exampleFix":"// before\n{{ amount|format_currency(currencyCode) }}\n// after\n{% if currencyCode matches '/^[A-Z]{3}$/' %}{{ amount|format_currency(currencyCode) }}{% endif %}","handlingStrategy":"try-catch","validationCode":"if (!preg_match('/^[A-Z]{3}$/', $currency) || !is_numeric($amount)) { throw new \\InvalidArgumentException('Bad currency/amount'); }","typeGuard":"function isFormattableCurrency(mixed $amount, string $currency): bool { return is_int($amount)||is_float($amount) && is_finite($amount) && preg_match('/^[A-Z]{3}$/', $currency) === 1; }","tryCatchPattern":"try { $s = format_currency($amount, $currency); } catch (Twig\\Error\\RuntimeError $e) { $s = number_format((float)$amount, 2).' '.$currency; }","preventionTips":["Validate ISO 4217 currency codes from config","Cast numeric strings to float/int","Keep the intl extension and ICU data up to date"],"tags":["intl","currency","formatter"],"backgroundTag":"invalid-argument-value","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}