I have found two ways of taking floors in Python:

3.1415 // 1

and

import math
math.floor(3.1415)

The problem with the first approach is that it return a float (namely 3.0). The second approach feels clumsy and too long.

Are there alternative solutions for taking floors in Python?

推荐答案

As long as your numbers are positive, you can simply convert to an int to round down to the next integer:

>>> int(3.1415)
3

For negative integers, this will round up, though.

Python-3.x相关问答推荐

Numba编译时间呈指数级增长--可以像C编译器一样配置优化级别吗?

动态范围内来自另外两列的列求和

从Metacritic上通过网络擦除游戏数据的问题

visual studio代码窗口中未激活虚拟环境11

将两列的乘积连续添加到一列的累积和中

以特定方式重新排列 pandas 数据框的列

Django在POST到外部URL时如何进行CSRF保护? 更新

从一列字符串中提取子字符串并将它们放入列表中

如何向 scikit-learn 函数添加类型提示?

从 yahoo Finance python 一次下载多只股票

Python:获取未绑定的类方法

python 3.7 websockets的1006连接异常关闭错误

UnicodeDecodeError:utf-8编解码器无法解码位置 1 的字节 0x8b:无效的起始字节,同时读取Pandas中的 csv 文件

如何确定一个类的元类?

pysftp vs. Paramiko

为什么`multiprocessing.Queue.get`这么慢?

try 在 Windows 10 高 DPI 显示器上解决模糊的 tkinter 文本 + zoom ,但担心我的方法不是 Pythonic 或不安全

为什么在 Python 3 中实例的 __dict__ 的大小要小得多?

__iter__ 和 __getitem__ 有什么区别?

在 Python 3 CGI 脚本中设置编码