Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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).

Expand Down
19 changes: 19 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,22 @@ def test_issue_1076_fixes():
assert numbers.format_compact_decimal(2345678, format_type="long", locale="ja") == "235万"
# Swahili does not define an accounting currency format; root aliases it to the standard format.
assert numbers.format_currency(-2, "USD", format_type="accounting", locale="sw") == "-US$\xa02.00"


@pytest.mark.parametrize(('locale', 'value', 'digits', 'expected'), [
('ru', 1100, 0, '1 тысяча'),
('ru', -1100, 0, '-1 тысяча'),
('ru', 1900, 0, '2 тысячи'),
('ru', 1010, 1, '1 тысяча'),
('ru', 1100, 1, '1,1 тысячи'),
('pl', 1100, 0, '1 tysiąc'),
('pl', 1900, 0, '2 tysiące'),
('cs', 1100, 0, '1 tisíc'),
('cs', 1900, 0, '2 tisíce'),
('fr', 1900, 0, '2 mille'),
('it', 1100, 0, 'mille'),
])
def test_compact_plural_uses_displayed_number(locale, value, digits, expected):
assert numbers.format_compact_decimal(
value, format_type='long', fraction_digits=digits, locale=locale,
) == expected
2 changes: 1 addition & 1 deletion tests/test_numbers_format_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def test_compact():
assert numbers.format_compact_decimal(21000000, locale='mk', format_type='long') == '21 милион'
assert numbers.format_compact_decimal(21345, locale="gv", format_type="short") == '21K'
assert numbers.format_compact_decimal(1000, locale='it', format_type='long') == 'mille'
assert numbers.format_compact_decimal(1234, locale='it', format_type='long') == '1 mila'
assert numbers.format_compact_decimal(1234, locale='it', format_type='long') == 'mille'
assert numbers.format_compact_decimal(1000, locale='fr', format_type='long') == 'mille'
assert numbers.format_compact_decimal(1234, locale='fr', format_type='long') == '1 millier'
assert numbers.format_compact_decimal(
Expand Down