int()向下取整:内置函数
round()四舍五入取整:内置函数,还可在保留x位小数的前提下四舍五入
>>> n=2.7562
>>> int(n)
2
>>> round(n)
3
>>>round(n,2)
2.76
floor() 向下取整 math模块函数
ceil()向上取整 math模块函数
>>> import math
>>> n=2.7
>>> math.floor(n)
2
>>> math.ceil(n)
3
modf() 分别取整数部分和小数部分 math模块函数
该方法返回一个包含小数部分和整数部分的元组
>>> x=2.5644
>>> math.modf(x)
(0.5644, 2.0)
>>>n=4.2
>>>math.modf(n)
(0.20000000000000018, 4.0)
最后一个输出涉及到了另一个问题,即浮点数在计算机中的表示,在计算机中是无法精确的表示小数的,至少目前的计算机做不到这一点。上例中最后的输出结果只是 0.2 在计算中的近似表示。Python 和 C 一样, 采用 IEEE 754 规范来存储浮点数。
摘自https://www.jb51.net/article/102248.htm
来源:CSDN
作者:HOLLAY
链接:https://blog.csdn.net/Hollay/article/details/104075126