Reputation: 2060
I hope I am making sense with this question.
Sometimes if I print a small float it'll look like 6.1248979238e-05
or something similar.
I want to be able to say "No matter what, output 10 digits precision like so": 0.abcdefghij
Upvotes: 3
Views: 1368
Reputation: 310079
>>> '%0.10f'% 6.1248979238e-05
'0.0000612490'
This is "string interpolation" or printf-style formatting which is still widely supported and used. Another option is the newer style string formatting:
>>> '{:.10f}'.format(6.1248979238e-05)
'0.0000612490'
Or if you want to be python2.6 compatible:
>>> '{0:.10f}'.format(6.1248979238e-05)
'0.0000612490'
Take your pick. Which version you use depends on which versions of python you want to support. If you're targeting only python2.7, it doesn't matter. If you want to support earlier versions use printf-style. I've personally switched to using the last form in most of my code. I don't care about python2.5 support, but python2.6 is still new enough that I'd like to support it where possible.
Upvotes: 7