diff --git a/functions/convert.py b/functions/convert.py index c6ed7ea..8d1931a 100644 --- a/functions/convert.py +++ b/functions/convert.py @@ -58,7 +58,7 @@ class Converter: for key in ['terms', 'privacy', 'timestamp']: data.pop(key, None) - if len(data.get('to')) == 0: + if not data.get('to'): raise RuntimeError('Failed to get the exchange rate from DuckDuckGo') conv = data.get('to')[0] diff --git a/main.py b/main.py index 5b54d05..bc807dd 100644 --- a/main.py +++ b/main.py @@ -78,12 +78,12 @@ async def currency(query: types.InlineQuery) -> None: )], query) - if not conv_currency or not from_currency: - return await reply(result_id, [('The currency exchange rate could not be found.', None, None)], query) - conv.from_currency = from_currency.upper() conv.conv_currency = conv_currency.upper() - await conv.convert() + try: + await conv.convert() + except RuntimeError as e: + return await reply(result_id, [('The currency exchange rate could not be determined', None, None)], query) chart = await create_chart(from_currency, conv_currency) diff --git a/utils/format_number.py b/utils/format_number.py index 6012c7f..7759bab 100644 --- a/utils/format_number.py +++ b/utils/format_number.py @@ -3,10 +3,12 @@ from decimal import Decimal def format_number(number): number = Decimal(str(number)) - formatted_integer_part = '{:,.0f}'.format(number).replace(',', ' ') + formatted_integer_part = '{:,.0f}'.format(number // 1).replace(',', ' ') + fractional_part = f"{number % 1:.30f}".split('.')[1] - if '.' in str(number): - fractional_part = str(number).split('.')[1][:3] - return formatted_integer_part + '.' + fractional_part - else: - return formatted_integer_part + if int(fractional_part) == 0: + return formatted_integer_part + '' + + significant_start = next((i for i, char in enumerate(fractional_part) if char != '0'), len(fractional_part)) + result_fractional = fractional_part[:significant_start + 3] + return formatted_integer_part + '.' + result_fractional