首页 > 技术文章 > python 取整

zoer 2020-06-27 17:01 原文

数据类型

整除

向下取整---//

-1//2,-7//2,-7//3,-7//4,-7//5
>>-1,-4,-3,-2,-2
1//2,7//2,7//3,7//4,7//5,7//6
>>0,3,2,1,1,1

截取整数---int()

int(0.3),int(1.3),int(1.7),int(1.5)
>>0,1,1,1
int(-0.3),int(-4.3)
>>0,-4

向上取整---math.ceil()

math.ceil(0.5),math.ceil(1.1),math.ceil(1.7),math.ceil(0.000011111)
>>1,2,2,1
math.ceil(-0.5),math.ceil(-1.1),math.ceil(-5.7),math.ceil(-0.0000111)
>>0,-1,-5,0

向下取整---math.floor(),等同于//

math.floor(0.5),math.floor(-0.5)
>>0,-1

四舍六入五取偶---round()

round(1.2),round(0.7),round(3.5),round(0.5)
>>1,1,4,0
round(-3.4),round(-3.999),round(-1.5)
>>-3,-4,2

隐式类型转换

isinstance(True,int),type(True),type(1+True),type(1+True+3.00)
>>True, bool, int, float

即使强类型语言,也存在隐式类型转换

推荐阅读