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
11 changes: 11 additions & 0 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,11 @@ def _get_compact_format(
pattern = parse_pattern(format).pattern
# if the pattern is "0", we do not divide the number
if pattern == "0":
rounded = round(number, fraction_digits)
if index > 0 and abs(rounded) >= magnitudes[index - 1]:
bumped = decimal.Decimal(magnitudes[index - 1]).copy_sign(number)
return _get_compact_format(bumped, compact_format, locale, fraction_digits)
number = rounded
break
# otherwise, we need to divide the number by the magnitude but remove zeros
# equal to the number of 0's in the pattern minus 1
Expand All @@ -703,6 +708,12 @@ def _get_compact_format(
format = compact_format[plural_form][str(magnitude)]
number = rounded
break
else:
rounded = round(number, fraction_digits)
if magnitudes and abs(rounded) >= magnitudes[-1]:
bumped = decimal.Decimal(magnitudes[-1]).copy_sign(number)
return _get_compact_format(bumped, compact_format, locale, fraction_digits)
number = rounded
return number, format


Expand Down
5 changes: 5 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ def test_format_compact_currency():
assert numbers.format_compact_currency(12345, 'EUR', locale='yav', format_type="short") == '€\xa012K'
assert numbers.format_compact_currency(123456789, 'EUR', locale='de_DE', fraction_digits=1) == '123,5\xa0Mio.\xa0€'
assert numbers.format_compact_currency(123456789, 'USD', locale='ar_EG', fraction_digits=2, format_type="short", numbering_system="default") == '\u200f123٫46\xa0مليون\xa0US$'
assert numbers.format_compact_currency(0.123, 'USD', locale='en_US') == '$0'
assert numbers.format_compact_currency(0.123, 'USD', locale='en_US', fraction_digits=2) == '$0.12'
assert numbers.format_compact_currency(999.6, 'USD', locale='en_US') == '$1K'
assert numbers.format_compact_currency(1234.5, 'JPY', locale='ja_JP') == '¥1234'
assert numbers.format_compact_currency(9999.6, 'JPY', locale='ja_JP') == '¥1万'


def test_format_compact_currency_invalid_format_type():
Expand Down
17 changes: 17 additions & 0 deletions tests/test_numbers_format_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@ def test_compact_rounding_carries_to_next_magnitude():
# Values that do not carry over are unaffected.
assert numbers.format_compact_decimal(999499, locale='en_US', format_type="short") == '999K'
assert numbers.format_compact_decimal(999949, locale='en_US', format_type="short", fraction_digits=1) == '999.9K'


def test_compact_fraction_digits_unscaled():
# Below the smallest compact magnitude.
assert numbers.format_compact_decimal(12.345, locale='en_US') == '12'
assert numbers.format_compact_decimal(12.345, locale='en_US', fraction_digits=2) == '12.34'
assert numbers.format_compact_decimal(12.34567, locale='en_US', fraction_digits=4) == '12.3457'
# Rounding below the smallest magnitude can carry into the first compact magnitude.
assert numbers.format_compact_decimal(999.6, locale='en_US') == '1K'
assert numbers.format_compact_decimal(-999.6, locale='en_US') == '-1K'
assert numbers.format_compact_decimal(999.6, locale='en_US', fraction_digits=1) == '999.6'
assert numbers.format_compact_decimal(999.96, locale='en_US', fraction_digits=1) == '1K'
# Locales with a "0" compact pattern at intermediate magnitudes.
assert numbers.format_compact_decimal(1234.5, locale='de_DE') == '1234'
assert numbers.format_compact_decimal(1234.5, locale='de_DE', fraction_digits=1) == '1234,5'
assert numbers.format_compact_decimal(999999.6, locale='de_DE') == '1\xa0Mio.'
assert numbers.format_compact_decimal(9999.6, locale='ja_JP') == '1万'