详解Python3中ceil()函数用法

yipeiwu_com5年前Python基础

描述

ceil(x) 函数返回一个大于或等于 x 的的最小整数。

语法

以下是 ceil() 方法的语法:

import math

math.ceil( x )

注意:ceil()是不能直接访问的,需要导入 math 模块,通过静态对象调用该方法。

参数

x -- 数值表达式。

返回值

函数返回返回一个大于或等于 x 的的最小整数。

实例

以下展示了使用 ceil() 方法的实例:

#!/usr/bin/python3
import math  # 导入 math 模块

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

python 向上取整ceil 向下取整floor 四舍五入round

#encoding:utf-8
import math

#向上取整
print "math.ceil---"
print "math.ceil(2.3) => ", math.ceil(2.3)
print "math.ceil(2.6) => ", math.ceil(2.6)

#向下取整
print "\nmath.floor---"
print "math.floor(2.3) => ", math.floor(2.3)
print "math.floor(2.6) => ", math.floor(2.6)

#四舍五入
print "\nround---"
print "round(2.3) => ", round(2.3)
print "round(2.6) => ", round(2.6)

#这三个的返回结果都是浮点型
print "\n\nNOTE:every result is type of float"
print "math.ceil(2) => ", math.ceil(2)
print "math.floor(2) => ", math.floor(2)
print "round(2) => ", round(2)

运行结果:

相关文章

Python字符串格式化

在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出。Python中内置有对字符串进行格式化的操作%。 模板 格式化字符串时,Python使用一个字符...

python实现上传样本到virustotal并查询扫描信息的方法

本文实例讲述了python实现上传样本到virustotal并查询扫描信息的方法。分享给大家供大家参考。具体方法如下: import simplejson import urlli...

numpy数组拼接简单示例

NumPy数组是一个多维数组对象,称为ndarray。其由两部分组成: ·实际的数据 ·描述这些数据的元数据 大部分操作仅针对于元数据,而不改变底层实际的数据。 关于NumPy数组有几点...

Python实现二维曲线拟合的方法

如下所示: from numpy import * import numpy as np import matplotlib.pyplot as plt plt.close() f...

Python使用functools模块中的partial函数生成偏函数

python 中提供一种用于对函数固定属性的函数(与数学上的偏函数不一样) # 通常会返回10进制 int('12345') # print 12345 # 使用参数 返回 8...