how to convert from Decimal('1.23456789') to Decimal('1.234')

alex23 wuwei23 at gmail.com
Mon Mar 23 03:01:04 EDT 2009


On Mar 23, 4:40 pm, valpa <valpass... at gmail.com> wrote:
> I only need the 3 digits after '.'
>
> Is there any way other than converting from/to string?

I'm not sure if this is the canonical way but it works:

>>> d = Decimal('1.23456789')
>>> three_places = Decimal('0.001') # or anything that has the exponent depth you want
>>> d.quantize(three_places, 'ROUND_DOWN')
Decimal('1.234')



More information about the Python-list mailing list