Skip to content

Choose unit plural forms from the displayed number - #1467

Open
0z5a wants to merge 1 commit into
python-babel:masterfrom
0z5a:fix/unit-plural-displayed-number
Open

0z5a wants to merge 1 commit into
python-babel:masterfrom
0z5a:fix/unit-plural-displayed-number

Conversation

@0z5a

@0z5a 0z5a commented Nov 15, 2026

Copy link
Copy Markdown

format_unit() picked the plural form of the unit pattern from the value as passed in, not from the number it actually prints. When the number pattern rounds the value or adds/drops fraction digits, the unit word disagrees with the number shown:

>>> from decimal import Decimal
>>> from babel.units import format_unit
>>> format_unit(0.9999, 'length-meter', locale='en')
'1 meters'
>>> format_unit(Decimal('1.0'), 'length-meter', locale='en')
'1 meters'
>>> format_unit(2.0001, 'length-meter', locale='pl')
'2 metra'
>>> format_unit(4.9999, 'duration-hour', locale='ru')
'5 часа'
>>> format_unit(1, 'length-meter', format='#,##0.00', locale='en')
'1.00 meter'

In CLDR a plural case "may depend on the formatting of those values" (UTS #35, Language Plural Rules), and the v/f operands count visible fraction digits; ICU selects the form from the formatted number. Intl.NumberFormat (Node 24, ICU 78) with style: 'unit', unitDisplay: 'long' gives 1 meter, 1 meter, 2 metry, 5 часов and 1.00 meters for the inputs above (passing the decimals as strings, so ICU sees the same digits).

This change parses the number pattern once in format_unit(), formats with it, and computes the plural operand the way NumberPattern.apply() renders the number: scale, round to the pattern's maximum fraction digits, and keep trailing zeros only down to its minimum fraction digits. format_compound_unit() goes through format_unit(), so it is fixed too (format_compound_unit(0.9999, 'kilometer', 1, 'hour', locale='en') gave '1 kilometers per hour').

Unchanged: preformatted string values (still treated as singular), values the pattern displays as-is (format_unit(1.5, 'length-meter', locale='pl') is still '1,5 metra'), non-finite values, and significant-digit and scientific patterns, which keep using the original value.

This is the same principle as #1301 and #1430 (compact decimal formats), applied to units. Comparing format_unit() with ICU for 16 locales, 4 units and 18 values (1152 cases), 236 differed before this change and none after.

The new test fails on main for the 6 affected cases (the other 6 are controls that already pass) and passes with the change; the full test suite passes (Python 3.12, macOS arm64, CLDR 49 imported with make import-cldr).

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

format_unit() picked the plural form from the value as passed in, not from
the number it prints, so 0.9999 m formatted as '1 meters' in English and
1 with '#,##0.00' as '1.00 meter'. Compute the plural operand the way the
number pattern renders the value (round to the maximum fraction digits,
keep trailing zeros down to the minimum), as ICU does.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants