Skip to content

Fix extract_operands() v and w for decimals with leading fractional zeros - #1489

Open
X6M-Work wants to merge 1 commit into
python-babel:masterfrom
X6M-Work:fix-plural-extract-operands-leading-zeros
Open

X6M-Work wants to merge 1 commit into
python-babel:masterfrom
X6M-Work:fix-plural-extract-operands-leading-zeros

Conversation

@X6M-Work

Copy link
Copy Markdown

Summary

babel.plural.extract_operands() computed wrong CLDR v and w operands for Decimal and float values with 0 <= |n| < 0.1 (for example, Decimal('0.05') returned v=1, w=1 instead of v=2, w=2, causing PluralRule({'one': 'v = 2'})(Decimal('0.05')) to return 'other').

Root cause

Decimal.as_tuple().digits omits leading zeros before the first significant digit (e.g. Decimal('0.005').as_tuple() has digits=(5,), exponent=-3). Slicing dec_tuple.digits[exp:] in babel/plural.py only yielded (5,), dropping -exp - len(fraction_digits) leading fractional zeros from trailing.

Solution

  • Prepend -exp - len(fraction_digits) leading '0's when building trailing in extract_operands().
  • Add test cases for Decimal('0.05'), 0.005, Decimal('0.00'), and Decimal('0.0050') in tests/test_plural.py.

Verification

  • pytest --noconftest tests/test_plural.py tests/test_plural_rule_parser.py -q: 46 passed (the 3 failures require built CLDR locale data via make import-cldr).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant