How to write string (float) to file?

Rainer Deyke root at rainerdeyke.com
Tue Aug 28 11:41:57 EDT 2001


<gbreed at cix.compulink.co.uk> wrote in message
news:9mfqmi$ko4$1 at thorium.cix.co.uk...
> In article <9mfm1l$gs3$1 at zeus.man.szczecin.pl>,
> piotrlg at sci.pam.szczecin.pl (Piotr Legiecki) wrote:
>
> > How can I tell python to give me only 2 digits after decimal point? And
> > of course store it in a variable.
> >
> > So I want 'c' to be 3.33.
>
> c = '%.2f'%(float(a)/b)

That's unreliable.  Use this instead:

c = (a * 100) / b
s = '%03d' % c
file.write(s[:-2] + '.' + s[-2:])


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