Repository navigation
Fix compact decimal plural selection after rounding - #1430
Conversation
|
可以的 我觉得你的想法很有道理
…---Original---
From: "Aarni ***@***.***>
Date: Wed, Nov 4, 2026 21:54 PM
To: ***@***.***>;
Cc: ***@***.******@***.***>;
Subject: Re: [python-babel/babel] Fix compact decimal plural selection after rounding (PR #1430)
@akx requested changes on this pull request.
In babel/numbers.py:
> @@ -692,8 +692,10 @@ def _get_compact_format( if index > 0 and abs(rounded) * divisor >= magnitudes[index - 1]: bumped = decimal.Decimal(magnitudes[index - 1]).copy_sign(number) return _get_compact_format(bumped, compact_format, locale, fraction_digits) - # if the remaining number is singular, use the singular format - plural_form = locale.plural_form(abs(scaled)) + # Select the plural form from the displayed number. Optional trailing + # fractional zeros are not rendered by the compact pattern. + plural_number = abs(rounded).normalize() + plural_form = locale.plural_form(plural_number) if plural_form not in compact_format: plural_form = "other" if scaled == 1 and "1" in compact_format:
Let's also use the rounded value here:
⬇️ Suggested change - if scaled == 1 and "1" in compact_format: + if rounded == 1 and "1" in compact_format:
and then change the tests accordingly and add
('fr', 1234, 0, 'mille'), ('fr', -1234, 0, '-1 millier'),
to the tests (this ought to match what Node ICU does).
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
|
|
@0718lol Sorry, I can't read that (without Google Translate anyway). Are you able to enact the changes requested? |
As requested in review: the special case should test the rounded display value instead of the scaled input, so e.g. fr 1234 with fraction_digits=0 formats as 'mille' (matching Node ICU), while fr -1234 keeps '-1 millier'. Adds the two French cases to test_compact_plural_uses_displayed_number and updates the matching expectation in test_compact.
|
Done, pushed as f6411c5:
Verification: the two new cases fail before the change and pass after. Full Sorry for the earlier reply in Chinese. This update was prepared with AI assistance under my direction, as disclosed in the PR description. |
There was a problem hiding this comment.
🟡 Changes recommended
The exact-1 pattern now applies to scaled values that round to 1, contradicting the description and requiring either a description update or restoration of the exact comparison.
1 open issue
What changed in this PR
Updates Babel’s compact decimal formatting to select plural forms from the rounded displayed value, addressing #1429.
Changes:
- Uses rounded values with optional trailing zeros removed for plural selection.
- Extends literal exact-
1pattern selection to values that round to 1. - Adds 13 regression cases across five locales and updates Italian and French expectations.
| File | Description |
|---|---|
| tests/test_numbers.py | Adds coverage for rounding, signs, retained fractions, and trimmed zeros. |
| tests/test_numbers_format_decimal.py | Updates Italian and French compact-format expectations. |
| babel/numbers.py | Uses normalized rounded values for plural rules and rounded values for exact-1 matching. |
Review effort: Balanced
|
Hi @akx, I've also updated the PR description to document the |
Otherwise e.g. French 1_100_000 fell back to "other" ("1 millions") instead of "one" ("1 million").
akx
left a comment
There was a problem hiding this comment.
Pushed another commit to fix one more corner case. Thanks.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1430 +/- ##
==========================================
+ Coverage 93.66% 93.69% +0.03%
==========================================
Files 27 27
Lines 4971 5047 +76
==========================================
+ Hits 4656 4729 +73
- Misses 315 318 +3 Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|

Fixes #1429.
Select compact decimal plural forms using the rounded displayed value rather than the unrounded scaled value. Normalize optional fractional zeros before plural selection, since they are not displayed. For example, Russian 1100 now renders as
1 тысячаinstead of1 тысячи, and Polish 1900 renders as2 tysiąceinstead of2 tysiąca.Also update the separate exact-
1pattern selection (if rounded == 1 and "1" in compact_format) per maintainer feedback so values that round to1match Node's Intl formatter (e.g., French1234->mille).Add 13 regression cases covering positive and negative values, retained fractions, trimmed zeros, and five locales. Update the existing Italian and French
1234expectations intests/test_numbers_format_decimal.pytomille.Validation: CLDR 48.2 imported using the repository script; Python 3.12.15 on macOS; full suite passed except pre-existing
test_input_paths_is_treated_as_listdue to--in local checkout directory name. Ruff andgit diff --checkpass. The new regression tests fail on the original implementation.Developed and tested with Codex assistance.