From b716256c1e7d4b9d3f6f7c2ce46d4a2dc1490d7a Mon Sep 17 00:00:00 2001 From: Nicolas Riebesel Date: Sat, 14 Sep 2019 13:24:32 +0200 Subject: [PATCH] Correct calculate_transaction --- abrechnung/group.py | 5 +++-- abrechnung/utils.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/abrechnung/group.py b/abrechnung/group.py index bb26afa..c096977 100644 --- a/abrechnung/group.py +++ b/abrechnung/group.py @@ -1,6 +1,7 @@ # vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 import random, copy, time import transaction as trans +import utils as u import operator class Group: @@ -141,12 +142,12 @@ def calculate_balancing(self): if pos.balance != 0: if pos.balance >= -neg.balance: #There is enough credit on the pos so neg has to transfer all to him - transaction_list.append(trans.Transaction(str(-neg.balance), neg.name, pos.name)) + transaction_list.append(trans.Transaction(u.amount_to_string(-neg.balance), neg.name, pos.name)) pos.balance += neg.balance neg.balance = 0 else: #There is not enough credit on pos so neg has to transfer a part to him - transaction_list.append(trans.Transaction(str(pos.balance), neg.name, pos.name)) + transaction_list.append(trans.Transaction(u.amount_to_string(pos.balance), neg.name, pos.name)) neg.balance += pos.balance pos.balance = 0 diff --git a/abrechnung/utils.py b/abrechnung/utils.py index 20116cd..8893a13 100644 --- a/abrechnung/utils.py +++ b/abrechnung/utils.py @@ -24,6 +24,22 @@ def levenshtein(s1, s2): return previous_row[-1] + +def amount_to_string(amount): + negative = amount < 0 + if negative: + amount *= -1 + + fractional_part = amount % 100 + integer_part = (amount - fractional_part) // 100 + + ret = "{}.{:02d}".format(integer_part, fractional_part) + if negative: + ret = "-" + ret + + return ret + + def parse_amount(s): try: return try_parse_with_delim(s, '.')