【Python】解决浮点数间运算存在不确定尾数的问题

#浮点数间运算存在不确定尾数,所以会输出False
if 0.1+0.2==0.3:
    print("Ture
")
else:
    print("False
")

存在上面的问题

解决方法:使用round()函数

#round(x,d)对X四舍五入,d是小数截取位数
if round(0.1+0.2,1)==0.3:
     print("Ture
")
else:
    print("False
")

原文地址:https://www.cnblogs.com/HGNET/p/12095504.html