twigphp/Twig · error · Twig\Error\RuntimeError

The number formatter rounding mode

Error message

The number formatter rounding mode "%s" does not exist, known modes are: "%s".

What it means

When the attribute 'rounding_mode' is set in $attrs, its value is validated against IntlExtension::NUMBER_ROUNDING_ATTRIBUTES. A value that is not a known rounding mode name throws this RuntimeError listing the valid modes.

Solutions

  1. Use one of the modes listed in the error (keys of NUMBER_ROUNDING_ATTRIBUTES, e.g. 'half_up', 'half_even', 'floor', 'ceiling')
  2. Fix the rounding mode string in the attrs array
  3. Validate configured rounding modes against NUMBER_ROUNDING_ATTRIBUTES keys
  4. Omit rounding_mode to keep the locale default

Example fix

// before
{{ n|format_number(attrs={'rounding_mode': 'ROUND_HALF_UP'}) }}
// after
{{ n|format_number(attrs={'rounding_mode': 'half_up'}) }}
Defensive patterns

Strategy: validation

Validate before calling

if (isset($attrs['rounding_mode']) && !isset(IntlExtension::NUMBER_ROUNDING_ATTRIBUTES[$attrs['rounding_mode']])) { throw new \InvalidArgumentException('Unknown rounding mode'); }

Type guard

function isKnownRoundingMode(string $m): bool { return isset(IntlExtension::NUMBER_ROUNDING_ATTRIBUTES[$m]); }

Try / catch

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

Prevention

When it happens

Trigger: Calling format_number/format_currency with attrs={'rounding_mode': X} where X is not a key of NUMBER_ROUNDING_ATTRIBUTES — e.g. 'half_up' misspelled as 'halfup', or a raw constant name like 'ROUND_HALFUP'.

Common situations: Using \NumberFormatter::ROUND_* constant names verbatim as strings; typos in template arguments; config-driven rounding modes not 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/cc10096471918657. Report an issue: GitHub.

Appendix: source

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

        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);
        }

        foreach ($textAttrs as $name => $value) {
            $this->numberFormatters[$hash]->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
        }

View on GitHub (pinned to a414c3a491)