python中的数字取整(ceil,floor,round)概念和用法
python中的数学运算函数(ceil,floor,round)的主要任务是截掉小数以后的位数.总体来说
就是取整用的。只是三者之间有微妙的区别:
floor() :把数字变小
ceil() : 把数字变大。
round() : 四舍五入。
英文不好的笔者,经常把这三者搞混,后来记着了三者的英文名字,就不会忘记了。
floor 是地板,
ceil 是天花板。
round 整整的,圆形的
再用一个简单的栗子加强记忆:
import math sample = 1.52 print "sample: %f ceil(sample): %f" % (sample,math.ceil(sample))
print "sample: %f floor(sample): %f" % (sample,math.floor(sample))
print "sample: %f round(sample): %f" % (sample,round(sample))
sample = 1.49 print "sample: %f ceil(sample): %f" % (sample,math.ceil(sample))
print "sample: %f floor(sample): %f" % (sample,math.floor(sample))
print "sample: %f round(sample): %f" % (sample,round(sample))
测试结果:
sample: 1.520000 ceil(sample): 2.000000
sample: 1.520000 floor(sample): 1.000000
sample: 1.520000 round(sample): 2.000000
sample: 1.490000 ceil(sample): 2.000000
sample: 1.490000 floor(sample): 1.000000
sample: 1.490000 round(sample): 1.000000
注意,这里的round不需要调用math库。
python中的数字取整(ceil,floor,round)概念和用法的更多相关文章
- PHP取整函数:ceil,floor,round,intval的区别详细解析
floor -- 舍去法取整说明float floor ( float value ) 返回不大于 value 的下一个整数,将 value 的小数部分舍去取整.floor() 返回的类型仍然是 fl ...
- python中的向上取整向下取整以及四舍五入的方法
import math #向上取整print "math.ceil---"print "math.ceil(2.3) => ", math.ceil(2. ...
- 关于取整函数ceil(),floor(),round()函数得应用
ceil()返回向上取整最接近的整数. double ceil(double); floor()返回向下取整最接近的整数. double floor(double); round()用于对浮点数的四舍 ...
- ABAP 向上取整和向下取整 CEIL & FLOOR
下面是一段关于CEIL 和 FLOOR 的代码 DATA:a TYPE mseg-menge, b TYPE mseg-menge, c TYPE mseg-menge. a = '1.36'. b ...
- AS3数字取整
AS3 数字取整方法int()去掉小数点trace(int(3.14)); //输出3trace(int(-3.14)); //输出-3Math.round()方法:Math.round()可以四舍五 ...
- C#以及Oracle中的上取整、下取整方法
1.C#中: 上取整——Math.Ceiling(Double),即返回大于或等于指定双精度浮点数的最大整数(也可称为取天板值): eg: Math.Ceiling(1.01)=2; Ma ...
- python(50):python 向上取整 ceil 向下取整 floor 四舍五入 round
取整:ceil 向下取整:floor 四舍五入:round 使用如下:
- 你可能不知道的 JavaScript 中数字取整
网上方法很多,标题党一下,勿拍 ^_^!实际开发过程中经常遇到数字取整问题,所以这篇文章收集了一些方法,以备查询. 常用的直接取整方法 直接取整就是舍去小数部分. 1.parseInt() parse ...
- c++ / % 四舍五入 向上取整ceil 向下取整floor
/ % 四舍五入 向上取整ceil 向下取整floor #include <math.h> double floor(double x); float floorf(float x); ...
随机推荐
- javascript函数嵌套时arguments的问题
疑问: var funtest = function () { var fun = function (val, val2) { alert(arguments.length); //此处答案? 有些 ...
- 关于spfa
关于spfa的一些事宜.... 刚开始学的时候只会跑最短路,代码都是背下来的.以下是背的代码... inline void spfa(int s) { queue<int>q;q.push ...
- springboot读取配置注解@ConfiguratioinProperties和@Value的区别
- python 迭代多个对象
并行迭代 zip for a,b,c in zip(list,list,tuple,list): print a,b,c 串行迭代 itertools.chain a = [1,2,3,4,5] b ...
- 深入JVM之类的加载过程
类的加载—连接—初始化 加载:查找并加载类的字节码文件,从硬盘到内存. 类的加载指的是将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个java.la ...
- python import hashllb
http://www.cnblogs.com/alex3714/articles/5161349.html 用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224 ...
- 探索未知种族之osg类生物--渲染遍历之GraphicsContext::runOperations
osg::GraphicsContext::runOperations().我们先来看一下这个函数的执行过程. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- Difference between MB Star C3 and MB Star C4
Many times ago, i saw a blog about MB sd connect C4 for benz, the author said he like this tool very ...
- boost学习 泛型编程之traits 学习
traits使用的场景一般有三种 分发到不同处理流程 解决C++代码中某些无法编译的问题 比如一个图书馆的代码,接受书籍并收入到不同类别中 template<class T> // T表 ...
- Android手机上浏览器不支持带端口号wss解决方案
首先抄个示例过来,命名为wss-test.html,然后传到服务器: <!DOCTYPE HTML> <html> <head> <meta http-equ ...