1.高斯取整
int取整相当于matlab里的高斯取整,对于一个数值,只取小数点前的整数,如:
In [70]: int(-3.14)
Out[70]: -3
In [71]: int(3.14)
Out[71]: 3
In [72]: int(-3.75)
Out[72]: -3
In [73]: int(3.75)
Out[73]: 3
2.四舍五入
round(x[, n]) 默认四舍五入整取, 可传入参数n,指定保留小数点后几位,特别地,当n=0,返回浮点类型的四舍五入整数
In [78]: round(3.14)
Out[78]: 3
In [79]: round(3.75)
Out[79]: 4
In [80]: round(3.14159,2)
Out[80]: 3.14
In [81]: round(3.14159,0)
Out[81]: 3.0
3.向上取整
向上和向下取整需要导入math模块,使用 math.ceil(x)
In [82]: math.ceil(3.14)
Out[82]: 4
In [83]: math.ceil(-3.14)
Out[83]: -3
4.向下取整
使用math.floor(x)
In [84]: math.floor(3.14)
Out[84]: 3
In [85]: math.floor(-3.14)
Out[85]: -4