简体   繁体   中英

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?

>>> "{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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM