[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-edu-sig
Subject:    [Edu-sig] Double inclusion
From:       dyoo () hkn ! eecs ! berkeley ! edu (Danny Yoo)
Date:       2001-10-01 20:38:28
Message-ID: Pine.LNX.4.21.0110011333270.31542-100000 () hkn ! eecs ! berkeley ! edu
[Download RAW message or body]

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!



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic