Simple string formatting question

Fredrik Lundh fredrik at pythonware.com
Fri Apr 7 02:34:38 EDT 2006


Paul McGuire wrote:

> Ooops, don't combine the two calls to rstrip().
>
> def format(f, width=3): return ("%.*f" % (width, f)).rstrip(".0")
>
> print format(3.140)
> print format(3.000)
> print format(3.0000001)
> print format(30.0000)
> print format(300000.000)

hey, I'm doing test-driven development.  being able to handle 30 and
300000 wasn't part of the original spec ;-)

> But this works fine:
> def format(f, width=3): return ("%.*f" % (width, f)).rstrip("0").rstrip(".")

indeed.

</F>






More information about the Python-list mailing list