From d275cee298f0f2e2ebfee946990e7eca318ec8f2 Mon Sep 17 00:00:00 2001 From: Matthew Alberto <62816261+matthewalberto@users.noreply.github.com> Date: Fri, 27 Nov 2026 10:11:35 +0800 Subject: [PATCH] Use the locale's minus sign for negative numbers Negative numbers were always printed with an ASCII "-", even in locales whose CLDR minus sign differs (U+2212 in fi, sv, nb and others, LRM + "-" in he and ar), and parse_decimal() and parse_number() rejected the locale's own minus sign. An unquoted "-" in a pattern affix now becomes the locale's minus sign for the numbering system in use, as UTS #35 specifies. The parse functions read that sign as well as "-", and strict mode compares both the same way. --- babel/numbers.py | 29 +++++++++++++++++--- tests/test_numbers.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/babel/numbers.py b/babel/numbers.py index f884b921d..10d6fd9b7 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -1120,6 +1120,8 @@ def parse_number( :raise `UnsupportedNumberingSystemError`: if the numbering system is not supported by the locale. """ group_symbol = get_group_symbol(locale, numbering_system=numbering_system) + minus_sign = get_minus_sign_symbol(locale, numbering_system=numbering_system) + string = string.replace(minus_sign, '-') if ( # if the grouping symbol is a kind of space, @@ -1189,6 +1191,10 @@ def parse_decimal( locale = Locale.parse(locale or LC_NUMERIC) group_symbol = get_group_symbol(locale, numbering_system=numbering_system) decimal_symbol = get_decimal_symbol(locale, numbering_system=numbering_system) + minus_sign = get_minus_sign_symbol(locale, numbering_system=numbering_system) + + # Accept the locale's own minus sign (U+2212 in Finnish, for example) as well as '-'. + string = string.replace(minus_sign, '-') if not strict and ( group_symbol in SPACE_CHARS # if the grouping symbol is a kind of space, @@ -1210,7 +1216,9 @@ def parse_decimal( decimal_quantization=False, numbering_system=numbering_system, ) - if string != proper and proper != _remove_trailing_zeros_after_decimal(string, decimal_symbol): # fmt: skip + # The string's minus sign was made '-' above, so compare it that way. + proper_ascii = proper.replace(minus_sign, '-') + if string != proper_ascii and proper_ascii != _remove_trailing_zeros_after_decimal(string, decimal_symbol): # fmt: skip try: parsed_alt = decimal.Decimal( string.replace(decimal_symbol, '').replace(group_symbol, '.'), @@ -1275,6 +1283,9 @@ def _remove_trailing_zeros_after_decimal(string: str, decimal_symbol: str) -> st return string +# A quoted literal (left as it is) or a '-' (the minus sign) in a pattern affix +_affix_minus_re = re.compile(r"'[^']*'|-") + _number_pattern_re = re.compile( r"(?P(?:[^'0-9@#.,]|'[^']*')*)" r"(?P[0-9@#.,E+]*)" @@ -1554,11 +1565,23 @@ def apply( numbering_system=numbering_system, ) + prefix = self.prefix[is_negative] + suffix = self.suffix[is_negative] + if '-' in prefix or '-' in suffix: + # An unquoted '-' in a pattern stands for the locale's minus sign. + minus_sign = get_minus_sign_symbol(locale, numbering_system=numbering_system) + + def localize_minus(match: re.Match[str]) -> str: + return minus_sign if match.group() == '-' else match.group() + + prefix = _affix_minus_re.sub(localize_minus, prefix) + suffix = _affix_minus_re.sub(localize_minus, suffix) + retval = ''.join( ( - self.prefix[is_negative], + prefix, number if self.number_pattern != '' else '', - self.suffix[is_negative], + suffix, ), ) diff --git a/tests/test_numbers.py b/tests/test_numbers.py index c7fef8563..646497e26 100644 --- a/tests/test_numbers.py +++ b/tests/test_numbers.py @@ -577,6 +577,67 @@ def test_parse_decimal_group_separator_can_be_any_space(string): assert decimal.Decimal('1099.98') == numbers.parse_decimal(string, locale='fr') +@pytest.mark.parametrize(('locale', 'kwargs', 'expected'), [ + ('en', {}, '-1,234.5'), + ('fi', {}, '\u22121\xa0234,5'), + ('sv', {}, '\u22121\xa0234,5'), + ('he', {}, '\u200e-1,234.5'), + ('fa', {}, '\u200e\u22121,234.5'), + ('ar', {'numbering_system': 'arab'}, '\u061c-1\u066c234\u066b5'), +]) +def test_format_decimal_uses_locale_minus_sign(locale, kwargs, expected): + assert numbers.format_decimal(-1234.5, locale=locale, **kwargs) == expected + + +def test_locale_minus_sign_in_other_formats(): + assert numbers.format_percent(-0.25, locale='fi') == '\u221225\xa0%' + assert numbers.format_percent(-0.25, locale='eu') == '\u2212%\xa025' + assert numbers.format_currency(-1234.5, 'SEK', locale='sv') == '\u22121\xa0234,50\xa0kr' + assert numbers.format_compact_decimal(-12345, locale='fi') == '\u221212\xa0t.' + # The mantissa and the exponent use the same sign + assert numbers.format_scientific(-0.00001234, locale='fi') == '\u22121,234E\u22125' + # The '-' of an explicit negative subpattern is the minus sign too + assert numbers.format_currency(-1234.5, 'ILS', locale='he') == '\u200f\u200e-1,234.50\xa0\u200f\u20aa' + assert numbers.format_decimal(-5, '0;0-', locale='fi') == '5\u2212' + + +def test_quoted_minus_in_pattern_is_literal(): + assert numbers.format_decimal(5, "'-'0", locale='fi') == '-5' + assert numbers.format_decimal(-5, "'-'0", locale='fi') == '\u2212-5' + + +@pytest.mark.parametrize('strict', [False, True]) +@pytest.mark.parametrize(('locale', 'string', 'expected'), [ + ('fi', '\u22121\xa0234,5', '-1234.5'), + ('fi', '-1\xa0234,5', '-1234.5'), + ('fi', '\u22121\xa0234,50', '-1234.5'), + ('he', '\u200e-1,234.5', '-1234.5'), + ('fa', '\u200e\u22121,234.5', '-1234.5'), +]) +def test_parse_decimal_locale_minus_sign(locale, string, expected, strict): + assert numbers.parse_decimal(string, locale=locale, strict=strict) == decimal.Decimal(expected) + + +def test_parse_decimal_strict_suggestions_use_locale_minus_sign(): + with pytest.raises(numbers.NumberFormatError) as excinfo: + numbers.parse_decimal('\u22121\xa023,4', locale='fi', strict=True) + assert excinfo.value.suggestions == ['\u2212123,4', '\u22121,234'] + + +def test_parse_number_locale_minus_sign(): + assert numbers.parse_number('\u22121\xa0234', locale='fi') == -1234 + assert numbers.parse_number('-1\xa0234', locale='fi') == -1234 + assert numbers.parse_number('\u200e-1,234', locale='he') == -1234 + + +@pytest.mark.all_locales +@pytest.mark.parametrize('numbering_system', ['latn', 'default']) +def test_negative_decimal_round_trip(locale, numbering_system): + formatted = numbers.format_decimal(-1234.5, locale=locale, numbering_system=numbering_system) + parsed = numbers.parse_decimal(formatted, locale=locale, numbering_system=numbering_system) + assert parsed == decimal.Decimal('-1234.5') + + def test_parse_grouping(): assert numbers.parse_grouping('##') == (1000, 1000) assert numbers.parse_grouping('#,###') == (3, 3)