如果我想在 Python 中将 float 转换为整数,我应该使用什么函数?
问题是我必须使用通过函数 math.fabs
传递的变量(绝对值)作为列表的索引,因此它必须是 int
,而函数 math.fabs
返回一个 float
。
最佳答案
使用 int()
构造函数:
>>> foo = 7.6
>>> int(foo)
7
请注意,如果您使用 built-in abs()
function使用整数参数,您首先会得到一个整数结果:
>>> type(abs(-7))
<type 'int'>
关于python - 在python中 float 为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621903/