Skip to content

Commit

Permalink
fix Locale.parse_money_amount()
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Apr 24, 2024
1 parent a9cc9a1 commit 5096062
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion liberapay/i18n/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,11 @@ def parse_money_amount(self, string, currency, maximum=D_MAX):
# https://github.com/liberapay/liberapay.com/issues/1066
if group_symbol in string:
proper = self.format_decimal(decimal, decimal_quantization=False)
if string != proper and string.rstrip('0') != (proper + decimal_symbol):
ambiguous = string != proper and (
(string + ('' if decimal_symbol in string else decimal_symbol)).rstrip('0') !=
(proper + ('' if decimal_symbol in proper else decimal_symbol)).rstrip('0')
)
if ambiguous:
# Irregular number format (e.g. `10.00` in German)
try:
proper_alt = (
Expand Down
12 changes: 12 additions & 0 deletions tests/py/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def test_locales_share_message_keys(self):
msgkey2 = self.website.locales['fr'].catalog['Save'].id
assert id(msgkey1) == id(msgkey2)

def test_parse_money_amount_accepts_valid_numbers(self):
assert LOCALE_EN.parse_money_amount("3400", 'USD')
assert LOCALE_EN.parse_money_amount("3,400", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.0", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.00", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.000", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.6", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.60", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.600", 'USD')
assert LOCALE_EN.parse_money_amount("3,400.65", 'USD')

def test_parse_money_amount_rejects_overly_precise_numbers(self):
with self.assertRaises(InvalidNumber):
LOCALE_EN.parse_money_amount("100.00001", 'EUR')
Expand Down

0 comments on commit 5096062

Please sign in to comment.