详解Python3中ceil()函数用法

yipeiwu_com6年前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 csv模块配置分隔符和引用符详解

如下所示: file = open('./abc.csv') csv.reader(file, delimiter=',', quotechar='"') 说明:delimiter...

让Django支持Sql Server作后端数据库的方法

好久木有在windows里面写python了。。这本也不是事儿,python嘛,只要系统里面装好了,那大多数代码都是可以正常运行的。。然而有时候需要调用微软那一套特定的API,什么Jav...

详解C++编程中一元运算符的重载

可重载的一元运算符如下: !(逻辑“非”) &(取址) ~(二进制反码) *(取消指针引用) +(一元加) -(一元求反) ++(递增) --(递减)...

python实现判断一个字符串是否是合法IP地址的示例

一个刚结束的笔试题目,简单贴一下吧,下面是具体实现: #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:判断一个...

解决python3 urllib 链接中有中文的问题

环境python3,开发平台pycharm,使用urllib时,当url中存在中文时会出现以下错误: UnicodeEncodeError: 'ascii' codec can't...