How to write string (float) to file?

Rainer Deyke root at rainerdeyke.com
Wed Aug 29 18:56:06 CEST 2001


"Grant Edwards" <grante at visi.com> wrote in message
news:slrn9oq5eu.leu.grante at grante.comtrol.com...
> In article <bo8j7.54122$c8.24122471 at news1.denver1.co.home.com>, Rainer
Deyke wrote:
> > "Grant Edwards" <grante at visi.com> wrote in message
> > news:slrn9ops86.ks3.grante at grante.comtrol.com...
> >> After reading a handful of follow-ups, I'm still curious what
> >> you mean when you say that's unreliable.
> >
> >>>> def foo(a, b): return '%.2f' % (float(a)/b)
> > ...

>
> Thanks, but that wasn't helpful.  Am I to assume that you're
> not happy with one of the results?  Which one?  Why do you
> think it's wrong?

> >>>> foo(1, 200)
> > '0.01'

Standard rounding: 0.01
Accountant rounding: 0.00

In this case the result resembles what you get with standard rounding, but
this is not always the case.  See below.

> >>>> foo(3, 200)
> > '0.01'

Standard rounding: 0.02
Accountant rounding: 0.02

This looks like a "just plain wrong" result to me.  If you don't care about
the least significant digit, this might be tolerable for you.

> >>>> foo(5, 200)
> > '0.03'

Standard rounding: 0.03
Accountant rounding: 0.02

Again, the result resembles what you get with standard rounding.  This case
doesn't really demonstrate anything new.

> >>>> foo(10000000000000000000000000000000000000000l, 2)
> > '5000000000000000200000000000000000000000.00'
                     ^

Not just incorrect, but the incorrect digit is right in the middle of the
number, not in the least significant place.


--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games           -           http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor





More information about the Python-list mailing list