详解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)

运行结果:

相关文章

详解Python3 中的字符串格式化语法

一、旧式的字符串格式化 % 操作符 参考以下示例: >>> name = "Eric" >>> "Hello, %s." % name 'He...

Python 3.x 判断 dict 是否包含某键值的实例讲解

查询资料得 Python 可以使用两种方式判断字典是否包含某键值 1、(dict.has_key('keyname')) 2、('keyname' in dict) 觉得第二种方式太过丑...

Python使用itchat模块实现群聊转发,自动回复功能示例

本文实例讲述了Python使用itchat模块实现群聊转发,自动回复功能。分享给大家供大家参考,具体如下: 1.itchat自动把好友发来的消息,回复给他 仅能实现自动回复 原文给 好友...

python和opencv实现抠图

本文实例为大家分享了python实现抠图的具体代码,供大家参考,具体内容如下 其中使用了opencv中的grabcut方法 直接上代码 # encoding:utf-8 # 图像提取...

python从子线程中获得返回值的方法

如下所示: # coding:utf-8 import time from threading import Thread def foo(number): time.s...