Python 中的 floor() 和 ceil() 函数
这两种方法是 Python math 模块的一部分,有助于获取分数的最近整数。
floor()
它接受一个带小数的参数,并返回一个小于该数字本身的整数。
语法
Syntax: floor(x) Where x is a numeric value
floor() 示例
在下面的示例中,我们采用不同类型的数值,如整数、正小数和负小数,并对它们应用 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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP