[英]How to print decimal notation rather than scientific notation in Python?
Assume to have this number:假设有这个数字:
p=0.00001
Now, if we print this variable we get 1e-05
(ie, that number in scientific notation).现在,如果我们打印这个变量,我们会得到1e-05
(即科学记数法中的那个数字)。 How can I print exactly 0.00001
(ie, in decimal notation)?我怎样才能精确地打印出0.00001
(即十进制)? Ok, I know I can use format(p, '.5f')
, but the fact is that I don't know in advance the number of decimal digits (eg, I may have 0.01
or 0.0000001
, etc.).好的,我知道我可以使用format(p, '.5f')
,但事实是我事先不知道小数位数(例如,我可能有0.01
或0.0000001
等)。 Therefore, I could count the number of decimal digits and use that in the format
function. In doing so, I have a related problem... is there any way to count the decimal digits of a decimal number without using Decimal
?因此,我可以计算小数位数并以 function format
使用它。这样做时,我遇到了一个相关问题......有没有办法在不使用Decimal
的情况下计算小数的小数位数?
... is there any way to count the decimal digits of a float number without using
Decimal
? ...有没有办法在不使用Decimal
的情况下计算浮点数的小数位数?
Nope.没有。 And there isn't any way to do it using Decimal
either.而且也没有任何方法可以使用Decimal
来做到这一点。 Floating point numbers that aren't an exact sum of powers of 1/2 don't have an exact number of decimal places.不是 1/2 幂的精确总和的浮点数没有精确的小数位数。 The best you can do is perform some sort of guesstimate based on the leftmost non-zero digit and some arbitrary count of digits.您能做的最好的事情就是根据最左边的非零数字和一些任意数字进行某种猜测。
In general you can't count the number of decimal digits, but you can compute the minimum precision and use it with f
format specification in str.format or equivalent function:一般来说,你不能计算小数位数,但你可以计算最小精度并将其与 str.format 中的f
格式规范或等效的function一起使用:
from math import ceil, log10
p = 0.00001
precision = int(ceil(abs(log10(abs(p))))) if p != 0 else 1
'{:.{}f}'.format(p, precision)
You'll need to increase precision if you want to display more than one significant digit.如果要显示一位以上的有效数字,则需要提高精度。 Also you might need a slightly different logic for numbers > 1.此外,对于大于 1 的数字,您可能需要稍微不同的逻辑。
As of Python 3.6.x and above versions, f-strings are a great new way to format strings.从 Python 3.6.x 及更高版本开始,f-strings 是一种很好的格式化字符串的新方法。 Not only are they more readable, more concise, and less prone to error than other ways of string formatting.与其他字符串格式化方式相比,它们不仅更易读、更简洁,而且更不容易出错。
You can read more about f-strings on https://datagy.io/python-f-strings/您可以在https://datagy.io/python-f-strings/上阅读更多关于 f-strings 的信息
Coming to your question,来到你的问题,
number = 0.00001 # 1e-5
print(f"{number:f}")
output: output:
0.000010
This also works directly eg这也可以直接使用,例如
number = 1e-5
print(f"{number:f}")
output: output:
0.000010
But this method can show you digits up to 1e-06但是这种方法可以显示最多 1e-06 的数字
for example例如
number = 0.000001 # 1e-6
print(f"{number:f}")
output: output:
0.000001
which works but if you go further哪个有效,但如果你进一步 go
number = 0.0000001 # 1e-7
print(f"{number:f}")
output: output:
0.000000
So be careful using this.所以要小心使用这个。
p = 0.0000000000000000000001
s = str(p)
print(format(p, "." + s.split("e")[-1][1:]+"f")) if "e" in s else print(p)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.