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)
Cee*_*man 108
以下是使用该format()
函数的示例:
>>> "{:.2E}".format(Decimal('40800000000.00000000000000'))
'4.08E+10'
Run Code Online (Sandbox Code Playgroud)
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
Eul*_*sel 26
没有人提到该.format
方法的简短形式:
至少需要Python 3.6
f"{Decimal('40800000000.00000000000000'):.2E}"
Run Code Online (Sandbox Code Playgroud)
(我相信它和Cees Timmerman一样,只是更短一些)
Jay*_*zzo 11
这是“简单”答案和评论的综合列表。
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)
这对我来说效果最好:
import decimal
'%.2E' % decimal.Decimal('40800000000.00000000000000')
# 4.08E+10
Run Code Online (Sandbox Code Playgroud)