[Baypiggies] rounding off problem

Jimmy Retzlaff jimmy at retzlaff.com
Wed Mar 23 19:30:18 CET 2011


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

> 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?


In the previous examples, everything was working properly but that doesn't
mean proper operation is always what people want for this. :)

The string format you show works fine for me for the values used earlier in
the thread:

>>> '%.3f' % 0.1
'0.100'
>>> '%.3f' % 0.5
'0.500'
>>> '%.3f' % round(3.5666666, 3)
'3.567'
>>> '%.3f' % 3.5666666
'3.567'
>>> '%.3f' % 3.2213333
'3.221'
>>> '%.3f' % 0.1
'0.100'

There can still be "surprises" here:

>>> '%.2f' % 2.675  # would you expect 2.68?
'2.67'

This is explained in the tutorial at
http://docs.python.org/tutorial/floatingpoint.html

Jimmy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20110323/a174641d/attachment.html>


More information about the Baypiggies mailing list