Skip to content

Respect fraction_digits for unscaled compact numbers - #1486

Open
seragAhmed wants to merge 1 commit into
python-babel:masterfrom
seragAhmed:fix/1484-compact-decimal-fraction-digits
Open

seragAhmed wants to merge 1 commit into
python-babel:masterfrom
seragAhmed:fix/1484-compact-decimal-fraction-digits

Conversation

@seragAhmed

Copy link
Copy Markdown

Fixes #1484.

_get_compact_format() now rounds to fraction_digits when formatting numbers below the smallest compact magnitude or matching a "0" compact pattern, instead of returning the unrounded Decimal directly. When rounding carries the value across a magnitude boundary (such as 999.6 to 1000 in en_US, or 9999.6 to 10000 in ja_JP), it re-runs the lookup at the next magnitude so the compact suffix is selected from the rounded value.

Verification

  • Added regression tests in tests/test_numbers_format_decimal.py and tests/test_numbers.py covering format_compact_decimal and format_compact_currency below the smallest magnitude, across magnitude carry boundaries, and on "0" patterns.
  • Ran pytest tests/test_numbers.py tests/test_numbers_format_decimal.py tests/test_support_format.py (159 passed) and ruff check.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be merged from my perspective: the #1484 repro at 931a39a matches ICU 78.3, carry cases included.

$ python repro.py   # 931a39a, CLDR from scripts/download_import_cldr.py
12          # 12.345, en_US
1K          # 999.6, en_US
$0          # 0.123 USD, en_US
1234        # 1234.5, de_DE
1万         # 9999.6, ja_JP
-1K         # -999.6, en_US
$0.12       # 0.125 USD, fraction_digits=2 (half-even)

$ python -m pytest -q
7004 passed, 1218 skipped, 1 xfailed

# babel/numbers.py reverted to aeca9fa, tests kept:
FAILED tests/test_numbers.py::test_format_compact_currency
FAILED tests/test_numbers_format_decimal.py::test_compact_fraction_digits_unscaled
2 failed, 139 passed

$ ruff check .   # ruff 0.14.10, the pre-commit pin
All checks passed!

Compact currency output changes for existing callers. Should CHANGES.rst say so?

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.

format_compact_decimal ignores fraction_digits below the smallest compact magnitude

3 participants