以科学计数法显示小数

Gre*_*reg 143 python string-formatting

我该如何显示:

十进制('40800000000.00000000000000')为'4.08E + 10'?

我试过这个:

>>> '%E' % Decimal('40800000000.00000000000000')
'4.080000E+10'
Run Code Online (Sandbox Code Playgroud)

但它有额外的0.

eum*_*iro 140

from decimal import Decimal

'%.2E' % Decimal('40800000000.00000000000000')

# returns '4.08E+10'
Run Code Online (Sandbox Code Playgroud)

在'40800000000.00000000000000'中,有许多更重要的零与任何其他数字具有相同的含义.这就是为什么你必须明确告诉你想要停止的地方.

如果要自动删除所有尾随零,可以尝试:

def format_e(n):
    a = '%E' % n
    return a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1]

format_e(Decimal('40800000000.00000000000000'))
# '4.08E+10'

format_e(Decimal('40000000000.00000000000000'))
# '4E+10'

format_e(Decimal('40812300000.00000000000000'))
# '4.08123E+10'
Run Code Online (Sandbox Code Playgroud)

  • 顺便说一句,尽管`format%values`语法仍然在Python 3标准库中使用,但我相信它在Python 3中已被弃用,或者至少不是推荐的格式化方法,以及当前推荐的语法,从Python开始2.6,将是`'{0:.2E}'.format(十进制('40800000000.00000000000000'))`(或Python 2.7+中的''{:.2E}'`.虽然对于这种情况并非严格有用,但由于没有附加功能的附加字符,`str.format`确实允许更复杂的混合/重新排列/重用图形参数. (21认同)
  • @CharlieParker使用[`format`](/sf/answers/1390499071/)。更*爵士*。 (3认同)
  • @MateenUlhaq f字符串是最好的:https://www.python.org/dev/peps/pep-0498/ (2认同)

Cee*_*man 108

以下是使用该format()函数的示例:

>>> "{:.2E}".format(Decimal('40800000000.00000000000000'))
'4.08E+10'
Run Code Online (Sandbox Code Playgroud)

  • 此语法也适用于3.6+`f“ {Decimal('40800000000.00000000000000'):. 2E}”`中的f字符串 (2认同)

pat*_*_ai 28

鉴于你的号码

x = Decimal('40800000000.00000000000000')
Run Code Online (Sandbox Code Playgroud)

从Python 3开始,

'{:.2e}'.format(x)
Run Code Online (Sandbox Code Playgroud)

是推荐的方法.

e意味着您需要科学记数法,并且.2意味着您需要点后2位数.所以你会得到x.xxE±n

  • 使用 Decimal 的目的是获得精确且任意精度的十进制算术。它不等同于使用浮动。 (2认同)

Eul*_*sel 26

没有人提到该.format方法的简短形式:

至少需要Python 3.6

f"{Decimal('40800000000.00000000000000'):.2E}"
Run Code Online (Sandbox Code Playgroud)

(我相信它和Cees Timmerman一样,只是更短一些)

  • 仅供像我这样的未来读者参考:如果您不关心控制位数也不介意浮点错误,您可以简单地使用“{num:E}”,其中例如 num = 40800000000.00000000000000 (4认同)
  • 应该接受答案。f-strings是python字符串格式化的未来:) (2认同)

Jay*_*zzo 11

这是“简单”答案和评论的综合列表。

蟒蛇3

from decimal import Decimal

x = '40800000000.00000000000000'
# Converted to Float
x = Decimal(x)

# ===================================== # `Dot Format`
print("{0:.2E}".format(x))
# ===================================== # `%` Format
print("%.2E" % x)
# ===================================== # `f` Format
print(f"{x:.2E}")
# =====================================
# ALL Return: 4.08E+10
print((f"{x:.2E}") == ("%.2E" % x) == ("{0:.2E}".format(x)))
# True
print(type(f"{x:.2E}") == type("%.2E" % x) == type("{0:.2E}".format(x)))
# True
# =====================================
Run Code Online (Sandbox Code Playgroud)

或 没有IMPORT

# NO IMPORT NEEDED FOR BASIC FLOATS
y = '40800000000.00000000000000'
y = float(y)

# ===================================== # `Dot Format`
print("{0:.2E}".format(y))
# ===================================== # `%` Format
print("%.2E" % y)
# ===================================== # `f` Format
print(f"{y:.2E}")
# =====================================
# ALL Return: 4.08E+10
print((f"{y:.2E}") == ("%.2E" % y) == ("{0:.2E}".format(y)))
# True
print(type(f"{y:.2E}") == type("%.2E" % y) == type("{0:.2E}".format(y)))
# True
# =====================================
Run Code Online (Sandbox Code Playgroud)

比较

# =====================================
x
# Decimal('40800000000.00000000000000')
y
# 40800000000.0

type(x)
# <class 'decimal.Decimal'>
type(y)
# <class 'float'>

x == y
# True
type(x) == type(y)
# False

x
# Decimal('40800000000.00000000000000')
y
# 40800000000.0
Run Code Online (Sandbox Code Playgroud)

因此,对于 Python 3,您现在可以在三者中的任何一个之间切换。

我的最爱:

print("{0:.2E}".format(y))
Run Code Online (Sandbox Code Playgroud)


Mih*_*eac 8

请参阅Python字符串格式的表格以选择正确的格式布局.在你的情况下它是%.2E.


Mat*_*tch 5

这对我来说效果最好:

import decimal
'%.2E' % decimal.Decimal('40800000000.00000000000000')
# 4.08E+10
Run Code Online (Sandbox Code Playgroud)