Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:In CLDR a plural case "may depend on the formatting of those values" (UTS #35, Language Plural Rules), and the
v/foperands count visible fraction digits; ICU selects the form from the formatted number.Intl.NumberFormat(Node 24, ICU 78) withstyle: 'unit', unitDisplay: 'long'gives1 meter,1 meter,2 metry,5 часовand1.00 metersfor 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 wayNumberPattern.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 throughformat_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.