from decimal import Decimal, ROUND_HALF_UP cent = Decimal("0.01") gross = 3 * Decimal("12.50") + 2 * Decimal("8.00") goods = (gross * Decimal("0.90")).quantize(cent, rounding=ROUND_HALF_UP) shipping = Decimal("5.00") if goods < Decimal("50.00") else Decimal("0.00") tax = (goods * Decimal("0.08")).quantize(cent, rounding=ROUND_HALF_UP) total = goods + shipping + tax assert gross - goods == Decimal("5.35") result = {"gross": str(gross), "discounted_goods": str(goods), "shipping": str(shipping), "tax": str(tax), "total": str(total)} import json print(json.dumps(result, ensure_ascii=False, sort_keys=True))