# [Baypiggies] rounding off problem

Vikram K kpguy1975 at gmail.com
Wed Mar 23 19:00:42 CET 2011

```I wish to avoid using print. The reason is that i wish to write these
formatted values to an output file. I tried this:

avg_3places =  '%.3f' % (float(avg))
std_dev_3places =  '%.3f' % (float(std_dev))

Some of the values are printed fine in the output file, but a few are not.
Of course, there were some conditional expressions used and i am checking my
code. But is it possible that python formatting in the way i have shown may
fail to work properly occasionally?

On Wed, Mar 23, 2011 at 12:33 PM, Stephen Cattaneo <
stephen.cattaneo at gmail.com> wrote:

> >>> 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/c20dbd9e/attachment.html>
```