ceil()方法函數返回x的值上限 - 小於x的最小整數。
語法
以下是 ceil() 方法的語法:
import math math.ceil( x )
注:此函數無法直接訪問,所以我們需要導入 math 模組,然後用數學靜態對象調用這個函數。
參數
-
x -- 這是一個數字運算式。
返回值
此方法返回不小於x的最小整數。
示例
下麵的示例說明 ceil() 方法的使用。
#!/usr/bin/python3 import math # This will import math module 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