[Tutor] Some questions about my yen-USD.py

Justin Ezequiel justin.mailinglists at gmail.com
Fri Sep 8 11:02:38 CEST 2006


Dick Moores <rdm <at> rcblue.com> writes:

> So now that I know better, I'm trying to write the beginnings of a
> general setPrecision() function using format strings. However, it
> appears that a variable cannot be used instead of the ".2" in
>
> "%.2f" % 2.234234
<snip>
> How to do this?

http://www.python.org/doc/current/lib/typesseq-strings.html

>>> "%.*f" % (2, 2.234234)
'2.23'
>>> "%.*f" % (3, 2.234234)
'2.234'
>>> "%.*f" % (1, 2.234234)
'2.2'
>>> "%.*f" % (0, 2.234234)
'2'


More information about the Tutor mailing list