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
2 changes: 1 addition & 1 deletion babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ def parse_decimal(
parsed = decimal.Decimal(string.replace(group_symbol, '').replace(decimal_symbol, '.'))
except decimal.InvalidOperation as exc:
raise NumberFormatError(f"{string!r} is not a valid decimal number") from exc
if strict and group_symbol in string:
if strict and (group_symbol in string or '_' in string):
proper = format_decimal(
parsed,
locale=locale,
Expand Down
10 changes: 10 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,16 @@ 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', 'grouped'), [('en_US', '1,000'), ('de', '1.000')])
def test_parse_decimal_strict_rejects_underscores(locale, grouped):
with pytest.raises(numbers.NumberFormatError):
numbers.parse_decimal('1_000', locale=locale, strict=True)

assert numbers.parse_decimal('1_000', locale=locale) == decimal.Decimal('1000')
assert numbers.parse_decimal('1000', locale=locale, strict=True) == decimal.Decimal('1000')
assert numbers.parse_decimal(grouped, locale=locale, strict=True) == decimal.Decimal('1000')


def test_parse_grouping():
assert numbers.parse_grouping('##') == (1000, 1000)
assert numbers.parse_grouping('#,###') == (3, 3)
Expand Down