How to truncate/round-off decimal numbers?
Scott David Daniels
scott.daniels at acm.org
Tue Jun 20 12:03:23 EDT 2006
Sybren Stuvel wrote:
> Girish Sahani enlightened us with:
>> I want to truncate every number to 2 digits after the decimal point....
>>>>> a = 2
>>>>> b = 3
>>>>> round(a*1.0 / b,2)
>> 0.67000000000000004
>
> If you want to format it, use '%.2f' % (float(a)/b)
Sybren has this right. If you follow everyone else's advice,
you'll eventually discover:
>>> print round(11024. / 5000.1, 2)
only gives you "2.2", not "2.20" (which is what, I suspect, you want).
--Scott David Daniels
scott.daniels at acm.org
More information about the Python-list
mailing list