Skip to content
Draft
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
29 changes: 26 additions & 3 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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, '.'),
Expand Down Expand Up @@ -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<prefix>(?:[^'0-9@#.,]|'[^']*')*)"
r"(?P<number>[0-9@#.,E+]*)"
Expand Down Expand Up @@ -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,
),
)

Expand Down
61 changes: 61 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down