decimal and trunkating

Marc Christiansen tolot at jupiter.solar-empire.de
Thu Jun 2 09:14:29 EDT 2005


Timothy Smith <timothy at open-networks.net> wrote:
> i want to trunkate 199.999 to 199.99
> getcontext.prec = 2 isn't what i'm after either, all that does is E's 
> the value.
> do i really have to use floats to do this?

You could try this (from a script I use for my phone bill):

from decimal import Decimal as d

def roundDecimal(num, prec):
    return d(num).quantize(d("1e%d" % (-prec)))

where `prec` is the number of places after the decimal point.

I'm sure there is a better solutions and someone will tell it, thereby
teaching us both. ;-)

Adiaŭ, Marc



More information about the Python-list mailing list