faqts : Computers : Programming : Languages : Python : Language and Syntax

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 15 people (93%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

How can I convert a floating point number to a string?

Mar 1st, 2000 22:06
Nathan Wallace, unknown unknown, Gerrit Holl, Carel Fellinger


>>> str(1.5)
'1.5'

You can use the builtin 'str' function if you want to convert _anything_
to a string: not only integers, lists, and dictionairies, but also file
objects and class instances. Please read the documentation for more
information.

If more control over the layout is needed you might use the "%" trickery
like:

>>> '%5.2f' % 123.4567
123.45
>>> '%5.2e' % 123.4567
1.23e+02