by Anonymous » 24 Aug 2025, 03:06
Ich versuche, Geldzahlen in Dezimalzahlen auf die nächste 0,05 zu runden. Im Moment mache ich das: < /p>
def round_down(amount):
amount *= 100
amount = (amount - amount % 5) / Decimal(100)
return Decimal(amount)
def round_up(amount):
amount = int(math.ceil(float(100 * amount) / 5)) * 5 / Decimal(100)
return Decimal(amount)
< /code>
Gibt es eine Möglichkeit, dies eleganter zu machen, ohne mit Python -Dezimalstellen umzugehen (vielleicht mit Quantize)? < /p>
Ich versuche, Geldzahlen in Dezimalzahlen auf die nächste 0,05 zu runden. Im Moment mache ich das: < /p>
def round_down(amount):
amount *= 100
amount = (amount - amount % 5) / Decimal(100)
return Decimal(amount)
def round_up(amount):
amount = int(math.ceil(float(100 * amount) / 5)) * 5 / Decimal(100)
return Decimal(amount)
< /code>
Gibt es eine Möglichkeit, dies eleganter zu machen, ohne mit Python -Dezimalstellen umzugehen (vielleicht mit Quantize)? < /p>