小编gur*_*101的帖子

圆形python十进制到最接近的0.05

我正试图将十进制的货币数量舍入到最接近的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处理浮点数(也许使用量化)?

python rounding

2
推荐指数
1
解决办法
1412
查看次数

标签 统计

python ×1

rounding ×1