Python float to string, only "dotted notation"?

429 Views Asked by At

I'm writing floating point numbers to a text file and I'd really appreciate if Python would stop writing numbers like "0.000002" as "2e-6". What can I do?

2

There are 2 best solutions below

7
On BEST ANSWER

You could use formatting directives, such as this one:

n = 0.000002
print('{:f}'.format(n))
0.000002

more information about formatting see these Python docs

Or old-style if working pre Python v2.6 (thanks @mgilson and @artSwri)

print('%f' % n)
1
On
>>> "%f" % 2e-6
'0.000002'