Python 浮点格式 - 类似“g”,但数字更多
我使用 "g"
来格式化浮点值,但它对我来说太快切换到科学格式 - 在第 5 位:
>>> format(0.0001, "g")
'0.0001'
>>> format(0.00001, "g")
'1e-05'
这似乎在 "g"
中进行了描述> 规则(-4):
精确的规则如下:假设用表示类型“e”和精度 p-1 格式化的结果将具有指数 exp。那么如果 -4 <= exp < p,数字采用表示类型“f”和精度 p-1-exp 进行格式化。否则,数字将采用表示类型“e”和精度 p-1 进行格式化。在这两种情况下,都会从尾数中删除无意义的尾随零,并且如果小数点后面没有剩余数字,则也会删除小数点。
有没有办法显示像 "g"
这样的数字,但在切换到科学记数法之前有更多的数字?
我正在考虑使用 ".6f"
并去掉尾随零,但这样我就看不到需要科学记数法的小数字了。
I use "g"
for formatting floating point values, but it switches to scientific formatting too soon for me - at the 5th digit:
>>> format(0.0001, "g")
'0.0001'
>>> format(0.00001, "g")
'1e-05'
This seems to be described in the "g"
rules (the -4):
The precise rules are as follows: suppose that the result formatted with presentation type 'e' and precision p-1 would have exponent exp. Then if -4 <= exp < p, the number is formatted with presentation type 'f' and precision p-1-exp. Otherwise, the number is formatted with presentation type 'e' and precision p-1. In both cases insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Is there a way to display numbers like "g"
, but with more digits before switching to scientific notation?
I'm thinking of using ".6f"
and stripping trailing zeros, but then I won't be able to see small numbers, which need scientific notation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也有同样的问题。
查看Python文档,似乎g也支持精度值:
我不知道为什么其他答案不使用这个,而且我在 Python 方面也不是很有经验,但它确实有效。
这可以通过使用
format(0.00001, '.10g')
轻松实现,其中 10 是您想要的精度。I had the same question.
Looking at the Python documentation it seems that g also supports precision values:
I don't know, why the other answers don't use this and I am not very experienced in Python, but it works.
This can be simply achieved by using
format(0.00001, '.10g')
where 10 is the precision you want.编辑:也可以将其放在一行上:
如果 n 可能为负数,则使用 log10(abs(n)) 代替 log10(n)。
编辑 2:根据 Adal 的评论进行改进:
这会将 0 打印为“0”。 - 如果您想要其他表示形式,例如“0”或“0.0”,则需要使用单独的
if< 对其进行特殊处理/代码>。
EDIT: it's also possible to put it on a single line:
If n might be negative, then use
log10(abs(n))
in place oflog10(n)
.EDIT 2: Improved based on Adal's comments:
This will print 0 as "0."--if you want another representation like "0" or "0.0", you'll need to special case it with a separate
if
.如果您使用的是 Python 2.7,您可以使用其高级字符串格式执行以下操作 迷你语言:
然后您可以动态指定所需的
number
和width
值。If you're using Python 2.7 you can do the following using it's advanced string formatting mini-language:
You can then specify the required value of
number
andwidth
dynamically.尝试:
如果abs(log10(n)) >= 5:
经过
#print("数字 N 是:","%e" % n)
#别的:
# print("数字 N 是:","%f" % n)
除了值错误:
print("数字 N 是:","%e" % n)
别的:
如果abs(log10(E)) >= 5:
print("数字 N 是:","%e" % n)
别的:
print("数字 N 是:","%f" % n)
try:
if abs(log10(n)) >= 5:
pass
#print("Number N is :","%e" % n)
#else:
# print("Number N is :","%f" % n)
except ValueError:
print("Number N is :","%e" % n)
else:
if abs(log10(E)) >= 5:
print("Number N is :","%e" % n)
else:
print("Number N is :","%f" % n)