详解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 pandas用法最全整理

1、首先导入pandas库,一般都会用到numpy库,所以我们先导入备用: import numpy as npimport pandas as pd 2、导入CSV或者xlsx文...

Python fileinput模块使用实例

fileinput模块可以对一个或多个文件中的内容进行迭代、遍历等操作。 该模块的input()函数有点类似文件readlines()方法,区别在于: 前者是一个迭代对象,即每次只生成一...

python处理两种分隔符的数据集方法

python处理两种分隔符的数据集方法

在做机器学习的时候,遇到这样一个数据集... 一共399行10列, 1-9列是用不定长度的空格分割, 第9-10列之间用'\t'分割, 前九列都是数值类型,其中第三列有若干个'?...

web.py中调用文件夹内模板的方法

web.py的模板使用非常容易,但有时候我们需要做目录层级以便管理。那么如何使用目录层级的模板呢? 通常我们使用模板的时候先声明模板文件夹 复制代码 代码如下: render=web.t...

Python3实现获取图片文字里中文的方法分析

本文实例讲述了Python3实现获取图片文字里中文的方法。分享给大家供大家参考,具体如下: 一、运行环境 (1) win10 (2) pycharm (3) python 3.5 (4)...