[Baypiggies] rounding off problem

Stephen Cattaneo stephen.cattaneo at gmail.com
Wed Mar 23 17:33:07 CET 2011


>>> x = 3.223344
>>> z = round(x,3)
>>> z
3.2229999999999999
>>> print z
3.223


On Wed, Mar 23, 2011 at 8:34 AM, Vikram K <kpguy1975 at gmail.com> wrote:

> 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
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>



-- 
---
Failures are finger posts on the road to achievement.

 -- C.S. Lewis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110323/0905f5d9/attachment.html>


More information about the Baypiggies mailing list