处理数据时,经常会遇到取整的问题,现总结如下
1,向下取整 int()
>>>a = 3.1
>>>b = 3.7
>>>int(a)
3
>>>int(b)
3
>>>int(-a)
-3
>>>int(-b)
-3
2,向上取整 math.ceil()
>>>from math import ceil
>>>a = 3.1
>>>b = 3.7
>>>ceil(a)
4
>>>ceil(b)
4
>>>ceil(-a)
-3
>>>ceil(-b)
-3
3,四舍五入 round()
>>>a = 3.123
>>>b = 3.256
>>>round(a)
3.0
>>>round(a, 2)
3.12
>>>round(b, 2)
3.26
>>>round(b, 1)
3.3