diff --git a/babel/numbers.py b/babel/numbers.py index 83424ec19..37743a448 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -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 @@ -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 diff --git a/tests/test_numbers.py b/tests/test_numbers.py index 5d293cb0b..c35b1f1f2 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -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(): diff --git a/tests/test_numbers_format_decimal.py b/tests/test_numbers_format_decimal.py index 124c5b96d..d1515b20f 100644 --- a/tests/test_numbers_format_decimal.py +++ b/tests/test_numbers_format_decimal.py @@ -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万'