在本教程中,我們將學習如何在 Python 中使用math
模塊的 floor()和 ceil()
函數。
樓層()功能:
floor()
函數用於獲取“X”的 floor 整數,表示最大的整數值,不大於“X”,基本上是其最近的向下舍入數。
語法:
math.floor(X)
參數:
我們可以傳遞數值表達式。
返回:
它返回不大於“X”的最大整數值。
讓我們看一個例子,了解一下如何在 Python 中實現 floor()
函數。
示例:
import math as M
# printing the floor value by using floor() function of math module
print ("The floor value of math.floor(-54.21) is: ", M.floor(-54.21))
print ("The floor value of math.floor(432.56) is: ", M.floor(432.56))
print ("The floor value of math.floor(320.62) is: ", M.floor(320.62))
輸出:
The floor value of math.floor(-54.21) is: -55
The floor value of math.floor(432.56) is: 432
The floor value of math.floor(320.62) is: 320
天花板()功能:
Python 中math
模塊的 ceil()
函數是用來得到“X”的天花板值作為回報的,表示最小的整數值,不小於“X”,基本上就是它最近的舍入數。
語法:
math.ceil(X)
參數:
我們可以傳遞數值表達式。
返回:
它返回不小於“X”的最小整數值。
讓我們看一個例子,了解一下如何在 Python 中實現 ceil()
函數。
示例:
import math as M
# printing the ceiling value by using ceil() function of math module
print ("The ceiling value of math.ceil(-54.21) is: ", M.ceil(-54.21))
print ("The ceiling value of math.ceil(432.56) is: ", M.ceil(432.56))
print ("The ceiling value of math.ceil(320.62) is: ", M.ceil(320.62))
輸出:
The ceiling value of math.ceil(-54.21) is: -54
The ceiling value of math.ceil(432.56) is: 433
The ceiling value of math.ceil(320.62) is: 321
結論
在本教程中,我們討論了如何在 Python 中實現math
模塊的 floor()和 ceil()
函數。
原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/130627.html