decimal and trunkating

Raymond Hettinger python at rcn.com
Fri Jun 3 08:34:52 CEST 2005


>> 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?

The precision is the total number of digits (i.e 199.99 has 5 digit
precision).  Either round to that precision level or use the quantize
method to round to a fixed number of places after the decimal point:


>>> Context(prec=5, rounding=ROUND_DOWN).create_decimal('199.999')
Decimal("199.99")
>>> Decimal('199.999').quantize(Decimal('0.01'), rounding=ROUND_DOWN)
Decimal("199.99")


Raymond Hettinger




More information about the Python-list mailing list