floor()和ceil()函数Python
这两种方法是pythonmath模块的一部分,该模块有助于获取小数的最接近的整数值。
地板()
它接受带有小数的数字作为参数,并返回小于数字本身的整数。
语法
Syntax: floor(x) Where x is a numeric value
例子floor()
在下面的示例中,我们采用不同类型的数值,例如整数,正十进制和负十进制,并将下限函数应用于它们。我们获得小于提供的数值的最接近的整数。
import math x,y,z = 21 , -23.6 , 14.2 print("The value of ",x, "on applying floor() function is:", math.floor(x)) print("The value of ",y, "on applying floor() function is:", math.floor(y)) print("The value of ",z, "on applying floor() function is:", math.floor(z))
输出结果
运行上面的代码给我们以下结果-
The value of 21 on applying floor() function is: 21 The value of -23.6 on applying floor() function is: -24 The value of 14.2 on applying floor() function is: 14
ceil()
它接受带有小数的数字作为参数,并返回大于数字本身的整数。
语法
Syntax: veil(x) Where x is a numeric value
例子ceil()
在下面的示例中,我们采用不同类型的数值,例如整数,正十进制和负十进制,并将ceil函数应用于它们。我们得到的最大整数大于提供的数值。
import math x,y,z = 21 , -23.6 , 14.2 print("The value of ",x, "on applying ceil() function is:", math.ceil(x)) print("The value of ",y, "on applying ceil() function is:", math.ceil(y)) print("The value of ",z, "on applying ceil() function is:", math.ceil(z))
输出结果
运行上面的代码给我们以下结果-
The value of 21 on applying ceil() function is: 21 The value of -23.6 on applying ceil() function is: -23 The value of 14.2 on applying ceil() function is: 15