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
8 changes: 5 additions & 3 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,11 +692,13 @@ 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:
if rounded == 1 and "1" in compact_format:
plural_form = "1"
if str(magnitude) not in compact_format[plural_form]:
plural_form = "other" # fall back to other as the implicit default
Expand Down
21 changes: 21 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,24 @@ 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'),
('fr', 1234, 0, 'mille'),
('fr', -1234, 0, '-1 millier'),
('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
4 changes: 2 additions & 2 deletions tests/test_numbers_format_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ 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(1234, locale='fr', format_type='long') == 'mille'
assert numbers.format_compact_decimal(
12345, format_type="short", locale='ar_EG', fraction_digits=2, numbering_system='default',
) == '12٫34\xa0ألف'
Expand Down