From eb4dd547a04f4770d7f3d2722887c1c18519bff0 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin <win10@list.ru> Date: Wed, 1 Jul 2020 11:34:02 +0400 Subject: [PATCH] Updated converter. Signed-off-by: Pavel Kirilin <win10@list.ru> --- src/actions/converter.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/actions/converter.py b/src/actions/converter.py index a03ea9e..f4b377b 100644 --- a/src/actions/converter.py +++ b/src/actions/converter.py @@ -1,6 +1,6 @@ import logging import re -from decimal import Decimal, getcontext +from decimal import Decimal import httpx from src.config import config @@ -71,22 +71,26 @@ async def replace_currency(event): groups = re.findall(CURRENCY_PATTERN, event.message.message) response = "**ПолагаÑÑÑŒ на текщий ÐºÑƒÑ€Ñ Ð²Ð°Ð»ÑŽÑ‚ могу Ñказать Ñледующее:**\n\n" currency_mapper = [] - getcontext().prec = 2 for group in groups: + scientific = False name = group[3] if name in CURRENCY_MAPPING: name = CURRENCY_MAPPING[name] val = Decimal(group[0]) + val_id = f"{val} {name}" + if len(f"{val:.2f}") > 30: + scientific = True new_val = ( val * Decimal(currencies[name]["Value"]) / Decimal(currencies[name]["Nominal"]) ) - val_id = f"{val} {name}" if val_id not in currency_mapper: - response += f"`{val} {name} = {new_val} RUB`\n" + if scientific: + response += f"`{val} {name} = {new_val.to_eng_string()} RUB`\n" + else: + response += f"`{val} {name} = {new_val:.2f} RUB`\n" currency_mapper.append(val_id) - await event.reply(response) -- GitLab