Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 看到长浮点数_Python_Floating Point - Fatal编程技术网

Python 看到长浮点数

Python 看到长浮点数,python,floating-point,Python,Floating Point,如果您输入一个很长的浮点并将其存储在某个变量x,如果您调用print(x)或类似的方法,您将得到一个较短的号码 有没有办法克服这个问题,查看变量中存储的所有数字,或者至少显示一定数量的数字 例如,假设我想要2的平方根: >>> import math >>> math.sqrt(2) 1.4142135623730951 >>> math.modf(math.sqrt(2)) (0.41421356237309515, 1.0) >&g

如果您输入一个很长的浮点并将其存储在某个变量
x
,如果您调用
print(x)
或类似的方法,您将得到一个较短的号码

有没有办法克服这个问题,查看变量中存储的所有数字,或者至少显示一定数量的数字

例如,假设我想要2的平方根:

>>> import math
>>> math.sqrt(2)
1.4142135623730951
>>> math.modf(math.sqrt(2))
(0.41421356237309515, 1.0)
>>> sqrtOfTwo=math.modf(math.sqrt(2))
>>> print(sqrtOfTwo[0]+sqrtOfTwo[1])
1.4142135623730951
当我调用
sqrt
时,我得到了一个小数点后16位的浮点值。然而,当我调用
modf
时,浮点显示为小数点后17位


假设我想看到2到20位小数的平方根。如何执行此操作?

要以给定精度显示浮动,可以使用
.format()
方法,如下所示:

print "{:.20f}".format(math.sqrt(2))
>>> 1.41421356237309514547
或者根据Lexy的评论,您也可以使用:

format(math.sqrt(2), ".20f")

或者调用类似的格式:
格式(math.sqrt(2),“.20f”)
。两者都是好的!或者
print“%.20f”%math.sqrt(2)
我知道我很麻烦,但是有没有办法在不使用int()函数的情况下将其显示为浮点数?顺便说一句,我现在想把你作为答案,但我不能。上面说我只能在5分钟内回答。@Z,对不起,我刚刚意识到那是不可能的。