python int() round() floor()函数的比较
2015-01-04 14:27
651 查看
猛的一看 int() round() math.floor() 这几个函数函数好像做的是同一件事情,很容易将他们弄混,下面是他们的一些不同之处:
int()函数直接截去小数部分
floor() 得到最接近原数但是小于原数的部分
round()得到最接近原数的整数(返回为浮点类型)
如下面的例子:
import math
for eachnum in (.2,.7,1.2,1.7,-.2,-.7,-1.2,-1.7):
print"int(%.1f)\t%+.1f"%(eachnum,float(int(eachnum)))
print"floor(%.1f)\t%+.1f"%(eachnum,math.floor(eachnum))
print "round(%.1f)\t%+.1f"%(eachnum,round(eachnum))
print'-'*20
>>>
int(0.2) +0.0
floor(0.2) +0.0
round(0.2) +0.0
--------------------
int(0.7) +0.0
floor(0.7) +0.0
round(0.7) +1.0
--------------------
int(1.2) +1.0
floor(1.2) +1.0
round(1.2) +1.0
--------------------
int(1.7) +1.0
floor(1.7) +1.0
round(1.7) +2.0
--------------------
int(-0.2) +0.0
floor(-0.2) -1.0
round(-0.2) -0.0
--------------------
int(-0.7) +0.0
floor(-0.7) -1.0
round(-0.7) -1.0
--------------------
int(-1.2) -1.0
floor(-1.2) -2.0
round(-1.2) -1.0
--------------------
int(-1.7) -1.0
floor(-1.7) -2.0
round(-1.7) -2.0
--------------------
>>>
转自: /article/8208675.html
int()函数直接截去小数部分
floor() 得到最接近原数但是小于原数的部分
round()得到最接近原数的整数(返回为浮点类型)
如下面的例子:
import math
for eachnum in (.2,.7,1.2,1.7,-.2,-.7,-1.2,-1.7):
print"int(%.1f)\t%+.1f"%(eachnum,float(int(eachnum)))
print"floor(%.1f)\t%+.1f"%(eachnum,math.floor(eachnum))
print "round(%.1f)\t%+.1f"%(eachnum,round(eachnum))
print'-'*20
>>>
int(0.2) +0.0
floor(0.2) +0.0
round(0.2) +0.0
--------------------
int(0.7) +0.0
floor(0.7) +0.0
round(0.7) +1.0
--------------------
int(1.2) +1.0
floor(1.2) +1.0
round(1.2) +1.0
--------------------
int(1.7) +1.0
floor(1.7) +1.0
round(1.7) +2.0
--------------------
int(-0.2) +0.0
floor(-0.2) -1.0
round(-0.2) -0.0
--------------------
int(-0.7) +0.0
floor(-0.7) -1.0
round(-0.7) -1.0
--------------------
int(-1.2) -1.0
floor(-1.2) -2.0
round(-1.2) -1.0
--------------------
int(-1.7) -1.0
floor(-1.7) -2.0
round(-1.7) -2.0
--------------------
>>>
转自: /article/8208675.html
相关文章推荐
- python int() round() floor()函数的比较
- python int() round() floor()函数的比较
- Oracle三个常见数字处理函数:Floor, Round, Trunc比较
- Python函数之 ceil, floor,round
- 比较Cint() , int() , fix() ,round()的区别
- 比较Cint() , int() , fix() ,round()的区别
- Javascript Math ceil()、floor()、round()三个函数的区别
- Javascript:三个函数ceil()、floor()、round()的区别
- Matlab里面的四个函数:Floor, Ceil, Fix, Round
- JS Math.max(),Math.random(), Math.round(),Math.ceil(),Math.floor()函数
- 比较Cint() , int() , fix() ,round()的区别
- 经常用到取整的函数,今天小小的总结一下!其实很简单,就是几个函数而已~~主要是:ceil,floor,round,intval
- sql中详解round(),floor(),ceiling()函数的用法和区别?
- 笔试题目3 字符串比较函数 int strcmp(const char* str1,const char *str2)
- matlab中fix, floor, ceil, round 函数的使用方法
- 比较Cint() , int() , fix() ,round()的区别
- PHP与JS---取整数方法int,celi,floor,round
- Matlab里四个函数:Floor, Ceil, Fix, Round
- PHP与JS---取整数方法int,celi,floor,round
- asp Fix、Int、Round、CInt函数使用说明