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位小數(shù)
1.22
“”"
從數(shù)學(xué)理論上來說,四舍五入,round(10.5,0)應(yīng)該進(jìn)位為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." 如果距離兩邊一樣遠(yuǎn),會保留到偶數(shù)的一邊。比如round(0.5)和round(-0.5)都會保留到0,而round(1.5)會保留到2。
“”"
round(10.500000001,0)
11.0
round(10.5,0)
10.0
計(jì)算結(jié)果精度與預(yù)期不符的解決方法:
>>> 10-9.9
0.09999999999999964
>>> (10*100-9.9*100)/100
0.1
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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