Skip to main content
 首页 » 编程设计

python之在python中 float 为整数

2025年05月04日17over140

如果我想在 Python 中将 float 转换为整数,我应该使用什么函数?

问题是我必须使用通过函数 math.fabs 传递的变量(绝对值)作为列表的索引,因此它必须是 int,而函数 math.fabs 返回一个 float。

请您参考如下方法:

使用 int() 构造函数:

>>> foo = 7.6 
>>> int(foo) 
7 

请注意,如果您使用 built-in abs() function使用整数参数,您首先会得到一个整数结果:

>>> type(abs(-7)) 
<type 'int'>