我正试图将十进制的货币数量舍入到最接近的0.05.现在,我这样做:
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)
Run Code Online (Sandbox Code Playgroud)
有没有办法,我可以更优雅地做到这一点,而无需使用python Decimals处理浮点数(也许使用量化)?