# 关于python3四舍五入函数的处理,示例:3.5 2.5
# 由于整数部分为偶数,并且小数部分只有0.5的情况下
# round()函数会近似到偶数部分(见原doc解释)
# 需要+1处理, 其余情况round()函数输出正常
# “values are rounded to the closest multiple of 10 to
# the power minus ndigits; if two multiples are equally
# close, rounding is done toward the even choice.”
# 如果距离两边一样远,会保留到偶数的一边
# 涉及浮点数存储精度不同,0.5存储约为0.499999···
n = float(input())
if int(n)%2 == 0 and n-int(n)==0.5 :
print(int(n)+1)
else:
print(round(n))
本文深入探讨了Python3中round()函数的特殊处理方式,特别是在处理小数部分为0.5的情况时,该函数会倾向于偶数进行四舍五入。文章通过示例代码展示了当整数部分为偶数时,如何手动调整结果以符合预期。
1603

被折叠的 条评论
为什么被折叠?



