From bbdc83c2a9a7b48859cd13aee7d91544c66955f2 Mon Sep 17 00:00:00 2001 From: X6 M Date: Tue, 24 Nov 2026 21:26:06 +0100 Subject: [PATCH] Fix extract_operands() v and w for decimals with leading fractional zeros --- babel/plural.py | 8 ++++++-- tests/test_plural.py | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/babel/plural.py b/babel/plural.py index 310f8bbd1..be7817e85 100644 --- a/babel/plural.py +++ b/babel/plural.py @@ -66,8 +66,12 @@ def extract_operands( if isinstance(n, decimal.Decimal): dec_tuple = n.as_tuple() exp = dec_tuple.exponent - fraction_digits = dec_tuple.digits[exp:] if exp < 0 else () - trailing = ''.join(str(d) for d in fraction_digits) + if exp < 0: + fraction_digits = dec_tuple.digits[exp:] + leading_zeros = -exp - len(fraction_digits) + trailing = '0' * leading_zeros + ''.join(str(d) for d in fraction_digits) + else: + trailing = '' no_trailing = trailing.rstrip('0') v = len(trailing) w = len(no_trailing) diff --git a/tests/test_plural.py b/tests/test_plural.py index 0c4f91fd7..be7fef1e9 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -223,6 +223,10 @@ def test_next_token_type_not_ok_and_value_ok(): (decimal.Decimal('1.230'), '1.230', 1, 3, 2, 230, 23), (-1, 1, 1, 0, 0, 0, 0), (1.3, '1.3', 1, 1, 1, 3, 3), + (decimal.Decimal('0.05'), '0.05', 0, 2, 2, 5, 5), + (0.005, '0.005', 0, 3, 3, 5, 5), + (decimal.Decimal('0.00'), '0.00', 0, 2, 0, 0, 0), + (decimal.Decimal('0.0050'), '0.0050', 0, 4, 3, 50, 5), )