Note: The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.
回答题目。。。如果使用
int(n)
,则它将向零截断。如果结果不是偶数,则添加一个:马蒂恩说得对。如果你想让一个整数取整到最接近的偶数,那么我会用这个:
浮点数只是近似值;2.85不能精确表示
略大于2.85。在
0.5和0.75可以用二元分数(分别为1/2和1/2+1/4)精确表示。在
round()
函数documents this explicitly:相关问题 更多 >
编程相关推荐