[Baypiggies] rounding off problem

Tung Wai Yip tungwaiyip at yahoo.com
Wed Mar 23 19:25:27 CET 2011


The floating point representation of decimals also troubles me a lot. I  
often output data in json format. The output is unsightly and in many case  
just waste network bandwidth and storage.

>>> json.dumps([1.0, 1.1, 10.1, 100.1])
'[1.0, 1.1000000000000001, 10.1, 100.09999999999999]'

If print can clean it up, why isn't it applied uniformly in other  
situations?

Just try it quickly in Javascript. It doesn't seems to suffer from similar  
problem.

Wai Yip




> 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


More information about the Baypiggies mailing list