[Python] Round python decimal to nearest 0.05

Discussão em 'Python' iniciado por Stack, Agosto 23, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to round money numbers in Decimal to the nearest 0.05. Right now, I'm doing this:

    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)


    Is there any way I can do this more elegantly without dealing with floats using python Decimals (using quantize perhaps)?

    Continue reading...

Compartilhe esta Página