From ddb954d6438a75b267b5d4c7ed9e10e45290df0a Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Wed, 1 Jul 2020 10:50:15 +0400
Subject: [PATCH] Fixed long arithmetics in coverter.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 src/actions/converter.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/actions/converter.py b/src/actions/converter.py
index 010b778..a03ea9e 100644
--- a/src/actions/converter.py
+++ b/src/actions/converter.py
@@ -1,5 +1,6 @@
 import logging
 import re
+from decimal import Decimal, getcontext
 
 import httpx
 from src.config import config
@@ -70,21 +71,20 @@ async def replace_currency(event):
     groups = re.findall(CURRENCY_PATTERN, event.message.message)
     response = "**Полагаясь на текщий курс валют могу сказать следующее:**\n\n"
     currency_mapper = []
-    inf = float("inf")
+    getcontext().prec = 2
     for group in groups:
         name = group[3]
         if name in CURRENCY_MAPPING:
             name = CURRENCY_MAPPING[name]
-        val = float(group[0])
-        new_val = val * currencies[name]["Value"] / currencies[name]["Nominal"]
-        val_id = f"{val:.2f} {name}"
+        val = Decimal(group[0])
+        new_val = (
+            val
+            * Decimal(currencies[name]["Value"])
+            / Decimal(currencies[name]["Nominal"])
+        )
+        val_id = f"{val} {name}"
         if val_id not in currency_mapper:
-            if new_val == inf:
-                response += (
-                    f"{val:.2f} {name} = там число реально огромное, мне лень считать."
-                )
-            else:
-                response += f"`{val:.2f} {name} = {new_val:.2f} RUB`\n"
+            response += f"`{val} {name} = {new_val} RUB`\n"
             currency_mapper.append(val_id)
 
     await event.reply(response)
-- 
GitLab