我从 2D 数组中获取一个 float ,当我调试它时,该数字在数组中显示为 1406711403.588。我得到这样的变量
t = 数组名称[0][0]
然后t就变成了1406711403.59,无论我使用什么格式或圆形方法,它总是这样。我在终端尝试过:
>>> t = round(1406711403.588, 3)
>>> print t
1406711403.59
>>> t = round(1406711403.588, 2)
>>> print t
1406711403.59
>>> round(1406711403.588, 3)
1406711403.588
>>>
为什么总是四舍五入到小数点后两位?如何保持原来的精度?
非常感谢!
最佳答案
这与print
有关,而不是round
。试试这个:
>>> round(1406711403.588, 3)
1406711403.5880001
>>> print round(1406711403.588, 3)
1406711403.59
print
将自动进行舍入,除非您指定其他内容。您可以像这样指定小数位数:
>>> print "%.3f" % (round(1406711403.588, 3))
1406711403.588
>>> print "%.5f" % (round(1406711403.588, 3))
1406711403.58800
关于variables - 变量总是四舍五入到小数点后两位 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25388894/