{"record":{"id":"b4ba7708ea8c97be","repo":"twigphp/Twig","slug":"unable-to-format-the-given-number","errorCode":null,"errorMessage":"Unable to format the given number.","messagePattern":"Unable to format the given number\\.","errorType":"exception","errorClass":"Twig\\Error\\RuntimeError","httpStatus":null,"severity":"error","filePath":"extra/intl-extra/IntlExtension.php","lineNumber":393,"sourceCode":"        $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;\n    }\n\n    public function formatNumberStyle(string $style, $number, array $attrs = [], string $type = 'default', ?string $locale = null): string\n    {\n        return $this->formatNumber($number, $attrs, $style, $type, $locale);\n    }\n\n    /**\n     * @param \\DateTimeInterface|string|null  $date     A date or null to use the current time\n     * @param \\DateTimeZone|string|false|null $timezone The target timezone, null to use the default, false to leave unchanged\n     */\n    public function formatDateTime(Environment $env, $date, ?string $dateFormat = null, ?string $timeFormat = null, string $pattern = '', $timezone = null, ?string $calendar = null, ?string $locale = null): string\n    {\n        $date = $env->getExtension(CoreExtension::class)->convertDate($date, $timezone);\n","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/extra/intl-extra/IntlExtension.php#L375-L411","documentation":"formatNumber() wraps NumberFormatter::format(); when it returns false — ICU could not format the given number for the requested style/locale — the extension throws this RuntimeError instead of outputting an empty/false value.","triggerScenarios":"Calling format_number() with a non-numeric value (NaN, INF, non-numeric string), or incompatible attrs/locale causing the formatter to fail.","commonSituations":"Passing null or user input that is not numeric; locale data missing in ICU; using scientific/ordinal styles with inputs ICU rejects (e.g. spelling-out with unsupported locales).","solutions":["Coerce to int/float and validate is_numeric($number) before formatting.","Check for NAN/INF with is_finite().","Simplify or remove custom $attrs that may break the formatter.","Verify the intl extension and ICU data are available (php --ri intl)."],"exampleFix":"// before\n{{ userInput|format_number }}\n// after\n{% if userInput is numeric %}{{ userInput|format_number }}{% else %}—{% endif %}","handlingStrategy":"validation","validationCode":"if (!is_numeric($number) || !is_finite((float)$number)) { throw new \\InvalidArgumentException('Number required'); }","typeGuard":"function isFormattableNumber(mixed $n): bool { return (is_int($n) || is_float($n)) && is_finite($n); }","tryCatchPattern":"try { $s = format_number($number, attrs: $attrs); } catch (Twig\\Error\\RuntimeError $e) { $s = (string) (float) $number; }","preventionTips":["Coerce user input with (float) before formatting","Reject NAN/INF values early","Avoid exotic attrs unless ICU behavior is verified"],"tags":["intl","number-format","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"}