commit e5abc1b45040ffb26408b439823a07a2217cee62 parent de86dc7d48959ac5f2f9eee2245df54ff8761333 Author: Marcello Stanisci <marcello.stanisci@inria.fr> Date: Mon, 20 Feb 2017 17:07:35 +0100 Amount subtraction. Diffstat:
| M | Python/pytaler/amounts.py | | | 26 | ++++++++++++++++++++++++++ |
1 file changed, 26 insertions(+), 0 deletions(-)
diff --git a/Python/pytaler/amounts.py b/Python/pytaler/amounts.py @@ -27,3 +27,29 @@ def amount_sum(a1, a2): "fraction": fractions % FRACTION, "currency": a1["currency"]} return ret + +# Computes a1 - a2 +def amount_sub(a1, a2): + assert(a1["currency"] == a2["currency"]) + + # Normalize + a1["value"] += int(a1["fraction"] / FRACTION) + a2["value"] += int(a2["fraction"] / FRACTION) + a1["fraction"] = a1["fraction"] % FRACTION + a2["fraction"] = a2["fraction"] % FRACTION + + # Extra care for fraction + if a1["fraction"] < a2["fraction"]: + a1["fraction"] += FRACTION + a1["value"] -= 1 + assert(a1["value"] >= 0) + + # Sub + ret = amount_get_zero(a1["currency"]) + ret["value"] = a1["value"] - a2["value"] + ret["fraction"] = a1["fraction"] - a2["fraction"] + + assert(ret["value"] >= 0) + assert(ret["fraction"] >= 0) + + return ret