Cutting off trailing zeroes?

Carel Fellinger cfelling at iae.nl
Sat Jan 29 07:34:13 EST 2000


Janos Blazi <jblazi at netsurf.de> wrote:
> Id like to format a string like this:
>   s = "(%f)" % x

> Now if x=1.5 then s="1.50000" or something like this. Is there a tirivial
> way of cutting of the zeroes, i.e. getting s="1.5" instead of s="1.50000"?

try the g and G specifiers like:

>>> "%g" % 1.5000
1.5
>>> "%g" % 1.50002
1.50002
>>> "%1.3g" % 1.50002
1.5

the default precision for g is, I think, .8. But it doesn't quit work like
advertised (on my system at least), somehow it influences the field width
as wel:( So you get 8 chars atmost, including the point and the numbers
before the point. So be carefull to avoid the following:

>>> "%1.3g" % 111.50002
112
-- 
groetjes, carel



More information about the Python-list mailing list