How to write string (float) to file?

Tim Peters tim.one at home.com
Tue Aug 28 21:58:40 CEST 2001


[Tim]
> ...
> My bet is the poster would rather, e.g., see '0.67' than '0.66' given
> a=2 and b=3.  If you're going to fake fp by hand, then you're
> responsible for faking all of it, incl. sensible rounding.

[Rainer Deyke]
> c = (((a * 200) / b) + 1) / 2
> s = '%03d' % c
> file.write(s[:-2] + '.' + s[-2:])

So that, e.g., 1/200 rounds to '0.01', but (-1)/200 rounds to '0.00' and
loses the minus sign?  '%.2f' looks better all the time <0.2f wink>.





More information about the Python-list mailing list