From 1e70503c405029e610fad115ab233a2e27bfd0a7 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 3 Nov 2017 17:40:42 +0100 Subject: don't use floats THIS BREAKS THE OLD BALANCES!!! --- geldschieberbot.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'geldschieberbot.py') diff --git a/geldschieberbot.py b/geldschieberbot.py index ee73877..33115e9 100644 --- a/geldschieberbot.py +++ b/geldschieberbot.py @@ -17,6 +17,23 @@ group_id = "GELD!!!" send_cmd = "signal-cli -u irgendwer send -g irgendwem" +def to_cent(euro): + euro = euro.split('.') + if len(euro) > 2: + raise TypeError + euro[0] = int(euro[0]) + if len(euro) < 2: + euro.append(0) + else: + if len(euro[1]) == 1: + euro[1] = int(euro[1]) * 10 + else: + euro[1] = int(euro[1]) + return euro[0] * 100 + euro[1] + +def to_euro(cents): + return str(cents/100) + def send(msg): subprocess.run(send_cmd.split(' '), input=msg.encode()) @@ -29,11 +46,11 @@ def create_summary(user): if amount == 0: continue total -= amount - summary += "\t{} {} {:.2}\n".format("<-" if amount < 0 else "->", person, abs(amount)) + summary += "\t{} {} {}\n".format("<-" if amount < 0 else "->", person, abs(to_euro(amount))) if summary == "": summary = "\tAll fine :)\n" else: - summary += "\tBalance: {:.2}".format(total) + summary += "\tBalance: {}".format(to_euro(total)) summary = user + ":\n" + summary return summary @@ -177,7 +194,7 @@ def handle_input(inp): sender = num2name[sender_number] try: - amount = float(amount) + amount = to_cents(amount) except: send("ERROR: amount musst be a number") continue @@ -196,7 +213,7 @@ def handle_input(inp): send("New Balance: {0} {1} {2:.2} {3}\n".format(sender, ("->" if p_balance > 0 else "<-"), - abs(p_balance), + abs(to_euro(p_balance)), recipient)) elif cmd == "!tanken": @@ -204,7 +221,7 @@ def handle_input(inp): send('ERROR: !tanken not in form "!tanken amount person [info]"') continue try: - amount = float(w[1]) + amount = to_cents(w[1]) except: send("ERROR: amount musst be a number") continue -- cgit v1.2.3