poc*_*coa 1 python math floating-point
>>> num = 4.123456
>>> round(num, 3) # expecting 4.123
4.1230000000000002
Run Code Online (Sandbox Code Playgroud)
结果我期待4.123,我错了吗?
是的,您的期望与您工具的设计意图不符.
查看Python教程的这一部分.
使用math.round
实际上非常罕见.如果你试图将数字显示为某个精度的字符串,你可能想要更像的东西
>>> num = 4.123456
>>> print "%.3f" % num
4.123
Run Code Online (Sandbox Code Playgroud)
您可能对有关字符串格式的文档感兴趣.