Python rounding to 05

Here in Switzerland they decided (for some reason, that can’t be good) not to pay the correct amount, but round it to the nearest 0/5 cent – which is a pain when doing python stuff. Therefore i made a Decimal rounding function for python, check it out:


from decimal import Decimal, ROUND_HALF_UP

def decimal_round(d, digits=0, to_five=True):
if to_five:
d = (d/Decimal("5"))
d = d.quantize(Decimal("1") / (Decimal('10') ** digits), rounding=ROUND_HALF_UP)
if to_five:
d = d * Decimal("5")
return d

Dette indlæg blev udgivet i Ikke kategoriseret. Bogmærk permalinket.

Skriv et svar

Din e-mail-adresse vil ikke blive offentliggjort. Krævede felter er markeret med *

*

Disse HTML koder og attributter er tilladte: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>