Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of money amounts #2347

Merged
merged 1 commit into from
Apr 24, 2024
Merged
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
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
Loading