Select the compact pattern from the rounded value - #1301
Open
Sreekant13 wants to merge 1 commit into
Open
Conversation
format_compact_decimal chose the magnitude from the unrounded number and only rounded afterwards, so a value that rounds up into the next magnitude kept the smaller unit: 999999 rendered as "1000K" instead of "1M", and likewise "1000M" for 999999999 and "1000 thousand" in the long format. The CLDR algorithm that _get_compact_format documents selects the pattern from the rounded value, so redo the lookup one magnitude up when rounding carries over. Values that do not carry over are unaffected.
Member
You should fix your environment. Unless, of course, this comment was maybe written by an agent in a sandbox. |
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_compact_decimalpicks the magnitude bucket from the unrounded number and only rounds afterwards, so a value that rounds up into the next magnitude keeps the smaller unit:"1000K" is not a valid compact form, since the mantissa is supposed to stay below the next magnitude. The CLDR algorithm that
_get_compact_formatlinks in its own docstring selects the pattern from the rounded value, so this redoes the lookup one magnitude up when rounding carries over.The retry goes through
_get_compact_formatagain rather than patching the pattern in place, so the next bucket's plural form is re-selected too; that is what makes the long format read "1 million" instead of "1 millions". It terminates because the bumped value sits exactly on the magnitude boundary and cannot carry a second time.Values that do not carry over are unaffected:
999->999,1000->1K,99999->100K,9999999->10M,999949withfraction_digits=1->999.9K,12345withfraction_digits=2->12.34K. Negatives keep their sign (-999999->-1M).Added
test_compact_rounding_carries_to_next_magnitude; it fails on the current code withassert '1000K' == '1M'.The number test files (143 tests), the
babel/numbers.pydoctests (40) and ruff all pass locally. Note thattests/test_dates.pyfails heavily in my environment withLookupError: Unknown timezonebecausetzdatais unavailable; those failures are identical with this change reverted and are unrelated to it.