How to suppress scientific notation when printing float values?

Matt picture Matt · Mar 18, 2009 · Viewed 180.7k times · Source

Here's my code:

x = 1.0
y = 100000.0    
print x/y

My quotient displays as 1.00000e-05.

Is there any way to suppress scientific notation and make it display as 0.00001? I'm going to use the result as a string.

Answer

Aziz Alto picture Aziz Alto · Oct 19, 2015

Using the newer version ''.format (also remember to specify how many digit after the . you wish to display, this depends on how small is the floating number). See this example:

>>> a = -7.1855143557448603e-17
>>> '{:f}'.format(a)
'-0.000000'

as shown above, default is 6 digits! This is not helpful for our case example, so instead we could use something like this:

>>> '{:.20f}'.format(a)
'-0.00000000000000007186'

Update

Starting in Python 3.6, this can be simplified with the new formatted string literal, as follows:

>>> f'{a:.20f}'
'-0.00000000000000007186'