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); ...
随机推荐
- Hibernate 再接触 总结
- javascript中的getter和setter
在ECMAScript 5中,属性值可以用一个或两个方法代替,这两个方法就是getter和setter var man = { name : 'lidg', weibo : '@lidg', get ...
- Java 集合类实现原理
转载自:http://blog.csdn.net/qq_25868207/article/details/55259978 :##ArrayList实现原理要点概括 参考文献:http://zhang ...
- 二十一、proxyDesign 代理模式
原理: 时序图: 代码清单: Printable public interface Printable { void setPrinterName(String name); String getPr ...
- python爬虫 scrapy框架(一)爬取壁纸照片
此项目仅供学习参考, 不用于任何商业用途 若侵权留言,立刻删除 刚入门爬虫不久,一心想找个网站试试,然后朋友推荐了这个壁纸网站
- HDU 5988 Coding Contest(最小费用最大流变形)
Problem DescriptionA coding contest will be held in this university, in a huge playground. The whole ...
- Pay attention to "Changing"
data l_ct_imseg type vsep_t_imseg. refresh l_ct_imseg. append lines of ct_imseg to l_ct_imseg. call ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
- 解决 ora-28001 密码过期的处理办法
转载自:https://blog.csdn.net/pengyouchuan/article/details/12905623 操作步骤: $sqlplus / as sysdba ALTER PRO ...
- Java并发集合(一)-CopyOnWriteArrayList分析与使用
CopyOnWriteArrayList分析与使用 原文链接: http://ifeve.com/java-copy-on-write/ 一.Copy-On-Write Copy-On-Write简称 ...