描述
ceil(x) 函數返回一個大于或等于 x 的的最小整數。
語法
以下是 ceil() 方法的語法:
import math math.ceil( x )
注意:ceil()是不能直接訪問的,需要導入 math 模塊,通過靜態對象調用該方法。
參數
x -- 數值表達式。
返回值
函數返回返回一個大于或等于 x 的的最小整數。
實例
以下展示了使用 ceil() 方法的實例:
#!/usr/bin/python3 import math # 導入 math 模塊 print ("math.ceil(-45.17) : ", math.ceil(-45.17)) print ("math.ceil(100.12) : ", math.ceil(100.12)) print ("math.ceil(100.72) : ", math.ceil(100.72)) print ("math.ceil(math.pi) : ", math.ceil(math.pi))
以上實例運行后輸出結果為:
math.ceil(-45.17) : -45 math.ceil(100.12) : 101 math.ceil(100.72) : 101 math.ceil(math.pi) : 4
python 向上取整ceil 向下取整floor 四舍五入round
#encoding:utf-8 import math #向上取整 print "math.ceil---" print "math.ceil(2.3) => ", math.ceil(2.3) print "math.ceil(2.6) => ", math.ceil(2.6) #向下取整 print "\nmath.floor---" print "math.floor(2.3) => ", math.floor(2.3) print "math.floor(2.6) => ", math.floor(2.6) #四舍五入 print "\nround---" print "round(2.3) => ", round(2.3) print "round(2.6) => ", round(2.6) #這三個的返回結果都是浮點型 print "\n\nNOTE:every result is type of float" print "math.ceil(2) => ", math.ceil(2) print "math.floor(2) => ", math.floor(2) print "round(2) => ", round(2)
運行結果:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

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