当前位置: 动力学知识库 > 问答 > 编程问答 >

floating point - How do I suppress scientific notation in Python?

问题描述:

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? How to convert the scientific notation into float.

Thanks in advance.

This feels somewhat ridiculous to ask but I haven't figured out

a way to do it yet. I'm going to use the result as a string.

网友答案:
'%f' % (x/y)

but you need to manage precision yourself. e.g.,

'%f' % (1/10**8)

will display zeros only.
details are in the docs

Or for Python 3 the equivalent old formatting or the newer style formatting

网友答案:

With newer versions of Python (2.6 and later), you can use ''.format() to accomplish what @SilentGhost suggested:

'{0:f}'.format(x/y)
网友答案:

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'
网友答案:

If it is a string then use the built in float on it to do the conversion for instance: print( "%.5f" % float("1.43572e-03")) answer:0.00143572

网友答案:

In addition to SG's answer, you can also use the Decimal module:

from decimal import Decimal
x = str(Decimal(1) / Decimal(10000))

# x is a string '0.0001'
分享给朋友:
您可能感兴趣的文章:
随机阅读: