1.9//2 #向下取整
0.0
1.9/2 #不取整
0.95
import math
math.floor(1/2) #向下取整
0
math.ceil(1/2) #向上取整
round(0.3) #四舍五入
0
round(0.8) #四舍五入
1
round(1.22222,2) #保留2位小數
1.22
“”"
從數學理論上來說,四舍五入,round(10.5,0)應該進位為11,
但是到了python3.5的doc中,文檔變成了"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." 如果距離兩邊一樣遠,會保留到偶數的一邊。比如round(0.5)和round(-0.5)都會保留到0,而round(1.5)會保留到2。
“”"
round(10.500000001,0)
11.0
round(10.5,0)
10.0
計算結果精度與預期不符的解決方法:
>>> 10-9.9
0.09999999999999964
>>> (10*100-9.9*100)/100
0.1
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
