python - 在python中 float 为整数

标签 python floating-point integer absolute-value

如果我想在 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/

相关文章:

python - DRF始终返回 "Authentication credentials were not provided"

floating-point - NVIDIA GPU 的 OpenCL 中浮点值的原子加法?

java - 重写 getColumnClass 方法后,jTable 仍然错误地对数字进行排序

python - 如何将此字符串列表转换为整数

python - OAuth API Google 云端硬盘 Python

python - 使用自定义资源子类方法的 Flask API

java - 长 float 输出显示字母

c - 在字符后读取多个数字

Python在目录中创建不需要的文件夹

python - 如何在python中将 float 转换为定点小数