[Edu-sig] Double inclusion

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Mon, 1 Oct 2001 13:38:28 -0700 (PDT)


On Mon, 1 Oct 2001, Titu Kim wrote:

>    I face a tricky question(at least for me it is) in
> regular string assignment. Let say i want to create a
> string representation of a floating number according
> to the decimal specification by user. For instance:
> a = 0.05
> format ="9.8f"
> b=?
> If i want to assign b with a string representation of
> variable a with the decimal format specified by
> 'format'. How can i do this? Thanks a lot.

Sounds like a job for Python's string formatting --- it should works very
well for this:

###
>>> a = 0.05
>>> format = "9.8f"
>>> b = ("%" + format) % a
>>> a
0.050000000000000003
###

You can find out more about string formatting here, in the "Fancier Output
Formatting" section of the Python tutorial:

http://www.python.org/doc/current/tut/node9.html#SECTION009100000000000000000

and String Formatting is explained in more detail in the library reference
here:

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

Good luck to you!