將浮點數限制為兩個小數點我想要a四舍五入13.95.>>> a13.949999999999999>>> round(a, 2)13.949999999999999這個round函數的工作方式與我預期的不一樣。
3 回答

胡子哥哥
TA貢獻1825條經驗 獲得超6個贊
>>> 125650429603636838/(2**53) 13.949999999999999 >>> 234042163/(2**24) 13.949999988079071 >>> a=13.946 >>> print(a) 13.946 >>> print("%.2f" % a) 13.95 >>> round(a,2) 13.949999999999999 >>> print("%.2f" % round(a,2)) 13.95 >>> print("{0:.2f}".format(a)) 13.95 >>> print("{0:.2f}".format(round(a,2))) 13.95 >>> print("{0:.15f}".format(round(a,2))) 13.949999999999999
添加回答
舉報
0/150
提交
取消