将浮点数向下舍入到最接近的整数?

2024-04-24 09:31:04 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

简单的

print int(x)

也会起作用的。

其中一个应该有效:

import math
math.trunc(1.5)
> 1
math.trunc(-1.5)
> -1
math.floor(1.5)
> 1
math.floor(-1.5)
> -2
x//1

//运算符返回除法的底。因为除以1不会改变您的数字,所以这相当于floor,但不需要导入。 注:

  1. 这将返回一个浮点值
  2. 这一轮朝-∞

相关问题 更多 >