Python3 ceil()函式

2019-10-16 23:11:51
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