如何抑制Python中的科学符号?

时间:2022-12-24 09:19:15

Here's my code:

这是我的代码:

x = 1.0
y = 100000.0    
print x/y

My quotient displays as 1.00000e-05

我的商显示为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.

有没有办法压制科学的符号,使其显示为0.00001?如何将科学符号转化为浮点数。

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.

提前谢谢。问这个问题有点荒谬,但我还没想好怎么做。我要用结果作为一个字符串。

8 个解决方案

#1


39  

'%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

对于Python 3,也可以使用等效的旧格式或更新的格式

#2


43  

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

使用新的Python版本(2.6和以后),您可以使用“.format()来完成@SilentGhost所建议的内容:

'{0:f}'.format(x/y)

#3


40  

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:

如上所示,默认是6位!这对我们的例子没有帮助,所以我们可以用这样的东西:

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

Update

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

从Python 3.6开始,可以使用新的格式化字符串文字对其进行简化,如下所示:

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

#4


3  

This will work for any exponent:

这适用于任何指数:

def getExpandedScientificNotation(flt):
    str_vals = str(flt).split('e')
    coef = float(str_vals[0])
    exp = int(str_vals[1])
    return_val = ''
    if int(exp) > 0:
        return_val += str(coef).replace('.', '')
        return_val += ''.join(['0' for _ in range(0, abs(exp - len(str(coef).split('.')[1])))])
    elif int(exp) < 0:
        return_val += '0.'
        return_val += ''.join(['0' for _ in range(0, abs(exp) - 1)])
        return_val += str(coef).replace('.', '')
    return return_val

#5


3  

This is using Captain Cucumber's answer, but with 2 additions.

这是用黄瓜船长的回答,但加了2个。

1) allowing the function to get non scientific notation numbers and just return them as is (so you can throw a lot of input that some of the numbers are 0.00003123 vs 3.123e-05 and still have function work.

1)允许函数得到非科学的表示法数字,并按原样返回(因此您可以大量输入其中一些数字是0.00003123 vs 3.123e-05,仍然有函数功。

2) added support for negative numbers. (in original function, a negative number would end up like 0.0000-108904 from -1.08904e-05)

2)增加对负数的支持。(在原来的函数中,负数从-1.08904e-05到0.0000-108904)

def getExpandedScientificNotation(flt):
    was_neg = False
    if not ("e" in flt):
        return flt
    if flt.startswith('-'):
        flt = flt[1:]
        was_neg = True 
    str_vals = str(flt).split('e')
    coef = float(str_vals[0])
    exp = int(str_vals[1])
    return_val = ''
    if int(exp) > 0:
        return_val += str(coef).replace('.', '')
        return_val += ''.join(['0' for _ in range(0, abs(exp - len(str(coef).split('.')[1])))])
    elif int(exp) < 0:
        return_val += '0.'
        return_val += ''.join(['0' for _ in range(0, abs(exp) - 1)])
        return_val += str(coef).replace('.', '')
    if was_neg:
        return_val='-'+return_val
    return return_val

#6


2  

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

除了SG的答案,你还可以使用Decimal模块:

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

# x is a string '0.0001'

#7


1  

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

如果它是一个字符串,那么使用内置的float来进行转换,例如:print(“%”)。5 f %浮动(“1.43572 e 03”))答:0.00143572

#8


0  

Using 3.6.4, I was having a similar problem that randomly, a number in the output file would be formatted with scientific notation when using this:

使用3.6.4,我遇到了一个类似的问题,在输出文件中,一个数字在使用时,会用科学的符号来格式化:

fout.write('someFloats: {0:0.8},{1:0.8},{2:0.8}'.format(someFloat[0], someFloat[1], someFloat[2]))

All that I had to do to fix it was to add 'f':

我要做的就是加上f:

fout.write('someFloats: {0:0.8f},{1:0.8f},{2:0.8f}'.format(someFloat[0], someFloat[1], someFloat[2]))

#1


39  

'%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

对于Python 3,也可以使用等效的旧格式或更新的格式

#2


43  

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

使用新的Python版本(2.6和以后),您可以使用“.format()来完成@SilentGhost所建议的内容:

'{0:f}'.format(x/y)

#3


40  

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:

如上所示,默认是6位!这对我们的例子没有帮助,所以我们可以用这样的东西:

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

Update

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

从Python 3.6开始,可以使用新的格式化字符串文字对其进行简化,如下所示:

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

#4


3  

This will work for any exponent:

这适用于任何指数:

def getExpandedScientificNotation(flt):
    str_vals = str(flt).split('e')
    coef = float(str_vals[0])
    exp = int(str_vals[1])
    return_val = ''
    if int(exp) > 0:
        return_val += str(coef).replace('.', '')
        return_val += ''.join(['0' for _ in range(0, abs(exp - len(str(coef).split('.')[1])))])
    elif int(exp) < 0:
        return_val += '0.'
        return_val += ''.join(['0' for _ in range(0, abs(exp) - 1)])
        return_val += str(coef).replace('.', '')
    return return_val

#5


3  

This is using Captain Cucumber's answer, but with 2 additions.

这是用黄瓜船长的回答,但加了2个。

1) allowing the function to get non scientific notation numbers and just return them as is (so you can throw a lot of input that some of the numbers are 0.00003123 vs 3.123e-05 and still have function work.

1)允许函数得到非科学的表示法数字,并按原样返回(因此您可以大量输入其中一些数字是0.00003123 vs 3.123e-05,仍然有函数功。

2) added support for negative numbers. (in original function, a negative number would end up like 0.0000-108904 from -1.08904e-05)

2)增加对负数的支持。(在原来的函数中,负数从-1.08904e-05到0.0000-108904)

def getExpandedScientificNotation(flt):
    was_neg = False
    if not ("e" in flt):
        return flt
    if flt.startswith('-'):
        flt = flt[1:]
        was_neg = True 
    str_vals = str(flt).split('e')
    coef = float(str_vals[0])
    exp = int(str_vals[1])
    return_val = ''
    if int(exp) > 0:
        return_val += str(coef).replace('.', '')
        return_val += ''.join(['0' for _ in range(0, abs(exp - len(str(coef).split('.')[1])))])
    elif int(exp) < 0:
        return_val += '0.'
        return_val += ''.join(['0' for _ in range(0, abs(exp) - 1)])
        return_val += str(coef).replace('.', '')
    if was_neg:
        return_val='-'+return_val
    return return_val

#6


2  

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

除了SG的答案,你还可以使用Decimal模块:

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

# x is a string '0.0001'

#7


1  

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

如果它是一个字符串,那么使用内置的float来进行转换,例如:print(“%”)。5 f %浮动(“1.43572 e 03”))答:0.00143572

#8


0  

Using 3.6.4, I was having a similar problem that randomly, a number in the output file would be formatted with scientific notation when using this:

使用3.6.4,我遇到了一个类似的问题,在输出文件中,一个数字在使用时,会用科学的符号来格式化:

fout.write('someFloats: {0:0.8},{1:0.8},{2:0.8}'.format(someFloat[0], someFloat[1], someFloat[2]))

All that I had to do to fix it was to add 'f':

我要做的就是加上f:

fout.write('someFloats: {0:0.8f},{1:0.8f},{2:0.8f}'.format(someFloat[0], someFloat[1], someFloat[2]))