Runde Python -Dezimalheit auf die nächste 0,05

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Runde Python -Dezimalheit auf die nächste 0,05

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>

Top