daxvena
daxvena

Reputation: 1150

How can I convert a float to a string, but to the sixth decimal - Python

I need to convert a float to a string, but to the sixth decimal. How would I preform a task like that without converting it into a string with an E notation?

Upvotes: 2

Views: 970

Answers (2)

sahhhm
sahhhm

Reputation: 5365

If you're on an older version of python:

print "%.6f" % 1.23456789

If you want the "e notation" you can try

print "%.6e" % 1.23456789

or

print "%.6E" % 1.23456789

Though I'm not sure now much flexibility you have with this method.

Upvotes: 1

robert
robert

Reputation: 34398

>>> "{0:.6f}".format(123455.12345678)
'123455.123457'

Note the rounding as well.

See the python docs on format string syntax and format specification mini-language.

Upvotes: 8

Related Questions