描述
ceil(x) 函數返回一個大于或等于 x 的的最小整數。
語法
以下是 ceil() 方法的語法:
import mathmath.ceil( x )
注意:ceil()是不能直接訪問的,需要導入 math 模塊,通過靜態對象調用該方法。
參數
x -- 數值表達式。
返回值
函數返回返回一個大于或等于 x 的的最小整數。
實例
以下展示了使用 ceil() 方法的實例:
#!/usr/bin/python3import 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) : -45math.ceil(100.12) : 101math.ceil(100.72) : 101math.ceil(math.pi) : 4
python 向上取整ceil 向下取整floor 四舍五入round
#encoding:utf-8import 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)
運行結果:

新聞熱點
疑難解答