Skip to content

Commit

Permalink
Ensure valid str->float conv in currency calc
Browse files Browse the repository at this point in the history
Currency amounts returned by google seem to randomly include unicode
chars ('\xa0' noted in #642) which broke the currency calculator
included in the project. This ensures that only strings that can be
converted to float are ever used in the conversion.

Fixes #642
  • Loading branch information
benbusby committed Feb 17, 2022
1 parent 0e711be commit 9984158
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions app/utils/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ def check_currency(response: str) -> dict:
currency2 = currency_link[1].text
currency1 = currency1.rstrip('=').split(' ', 1)
currency2 = currency2.split(' ', 1)

# Handle differences in currency formatting
# i.e. "5.000" vs "5,000"
if currency2[0][-3] == ',':
currency1[0] = currency1[0].replace('.', '')
currency1[0] = currency1[0].replace(',', '.')
Expand All @@ -250,10 +253,17 @@ def check_currency(response: str) -> dict:
else:
currency1[0] = currency1[0].replace(',', '')
currency2[0] = currency2[0].replace(',', '')
return {'currencyValue1': float(currency1[0]),
'currencyLabel1': currency1[1],
'currencyValue2': float(currency2[0]),
'currencyLabel2': currency2[1]

currency1_value = float(re.sub(r'[^\d\.]', '', currency1[0]))
currency1_label = currency1[1]

currency2_value = float(re.sub(r'[^\d\.]', '', currency2[0]))
currency2_label = currency2[1]

return {'currencyValue1': currency1_value,
'currencyLabel1': currency1_label,
'currencyValue2': currency2_value,
'currencyLabel2': currency2_label
}
return {}

Expand Down

0 comments on commit 9984158

Please sign in to comment.