twigphp/Twig · error · Twig\Error\RuntimeError

The number formatter attribute

Error message

The number formatter attribute "%s" does not exist, known attributes are: "%s".

What it means

createNumberFormatter validates each entry of the $attrs array against IntlExtension::NUMBER_ATTRIBUTES. An attribute name not present in that map throws this RuntimeError listing the recognized attribute names.

Solutions

  1. Use only attribute names listed in the error message (keys of NUMBER_ATTRIBUTES, e.g. 'fraction_digit')
  2. Rename the attribute key in the attrs array to a known one
  3. Validate attribute keys against NUMBER_ATTRIBUTES before calling the filter
  4. Check the IntlExtension docs/tests for the exact accepted attribute strings

Example fix

// before
{{ n|format_number(attrs={'max_fraction_digits': 2}) }}
// after
{{ n|format_number(attrs={'fraction_digit': 2}) }}
Defensive patterns

Strategy: validation

Validate before calling

$bad = array_diff(array_keys($attrs), array_keys(IntlExtension::NUMBER_ATTRIBUTES)); if ($bad) { throw new \InvalidArgumentException('Unknown attrs: '.implode(',', $bad)); }

Type guard

function hasKnownNumberAttrs(array $attrs): bool { return [] === array_diff(array_keys($attrs), array_keys(IntlExtension::NUMBER_ATTRIBUTES)); }

Try / catch

try { $out = $intl->formatNumber($n, $style, $attrs); } catch (\Twig\Error\RuntimeError $e) { if (str_contains($e->getMessage(), 'attribute')) { $out = $intl->formatNumber($n, $style, []); } else { throw $e; } }

Prevention

When it happens

Trigger: Calling format_number or format_currency with attrs containing an unknown key — e.g. {'max_fraction_digits': 2} instead of 'fraction_digit'/'rounding_mode' style names supported by the extension.

Common situations: Guessing attribute names based on \NumberFormatter PHP constants (MAX_FRACTION_DIGITS) instead of the extension's accepted string keys; config-driven attribute maps that were never validated.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

            foreach (self::NUMBER_SYMBOLS as $name => $const) {
                $symbols[$name] = $this->numberFormatterPrototype->getSymbol($const);
            }
        }

        ksort($attrs);
        $hash = $locale.'|'.$style.'|'.json_encode($attrs).'|'.json_encode($textAttrs).'|'.json_encode($symbols);

        if (!isset($this->numberFormatters[$hash])) {
            if (\count($this->numberFormatters) >= self::MAX_CACHED_FORMATTERS) {
                array_shift($this->numberFormatters);
            }
            $this->numberFormatters[$hash] = new \NumberFormatter($locale, self::NUMBER_STYLES[$style]);
        }

        foreach ($attrs as $name => $value) {
            if (!isset(self::NUMBER_ATTRIBUTES[$name])) {
                throw new RuntimeError(\sprintf('The number formatter attribute "%s" does not exist, known attributes are: "%s".', $name, implode('", "', array_keys(self::NUMBER_ATTRIBUTES))));
            }

            if ('rounding_mode' === $name) {
                if (!isset(self::NUMBER_ROUNDING_ATTRIBUTES[$value])) {
                    throw new RuntimeError(\sprintf('The number formatter rounding mode "%s" does not exist, known modes are: "%s".', $value, implode('", "', array_keys(self::NUMBER_ROUNDING_ATTRIBUTES))));
                }

                $value = self::NUMBER_ROUNDING_ATTRIBUTES[$value];
            } elseif ('padding_position' === $name) {
                if (!isset(self::NUMBER_PADDING_ATTRIBUTES[$value])) {
                    throw new RuntimeError(\sprintf('The number formatter padding position "%s" does not exist, known positions are: "%s".', $value, implode('", "', array_keys(self::NUMBER_PADDING_ATTRIBUTES))));
                }

                $value = self::NUMBER_PADDING_ATTRIBUTES[$value];
            }

            $this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
        }

View on GitHub (pinned to a414c3a491)