python学习笔记:取整函数

三种取整方式:

一、向下取整:即舍去小数点后所有数据。int (n),例如:

int(3.67)     #figure out 3

二、四舍五入:round(n),例如:

round(4.56)     #figure out 5
round(-4.56)    #figure out -5
round(-4.23)    #figure out -4

**round函数并不精确,问题详见“python笔记:round()函数及相关

三、向上取整:即舍去小数点后所有数据,并向上进1。import math  math.ceil(n),例如:

import math
math.ceil(5.44)    #figure out 6
math.ceil(5.56)    #figure out 6
math.ceil(-4.56)   #figure out -4

猜你喜欢

转载自blog.csdn.net/xiaozhimonica/article/details/82898062