[Baypiggies] rounding off problem

Vikram K kpguy1975 at gmail.com
Wed Mar 23 16:34:44 CET 2011


how does one round off a float number in python? i have tried this using two
ways but have been unsuccessful so far:

1. IDLE 2.6.6      ==== No Subprocess ====
>>> x = 3.5666666
>>> x = round(x,3)
>>> x
3.5670000000000002

[ The above works when i try doing x = round(x,2). I am working on a windows
machine. The above seems to work on the linux machine of a colleague but he
gets the same problem when he tries x = round(x,4)]

2. >>> from decimal import *
>>> getcontext().prec = 3
>>> x = decimal(3.456654)

>>> x = Decimal('3.2213333')
>>> x
Decimal('3.2213333')
>>> getcontext().prec = 5
>>> x
Decimal('3.2213333')
>>> y = round(x,3)
>>> y
3.2210000000000001
>>> x = 3.223344
>>> z = round(x,3)
>>> z
3.2229999999999999
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110323/b4f6998c/attachment.html>


More information about the Baypiggies mailing list