Python 将浮点数向下舍入到最近的整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17141979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Round a Floating Point Number Down to the Nearest Integer?
提问by Anthony Perez
As the title suggests, I want to take a floating point number and round it down to the nearest integer. However, if it's not a whole, I ALWAYS want to round down the variable, regardless of how close it is to the next integer up. Is there a way to do this?
正如标题所暗示的,我想取一个浮点数并将其四舍五入到最接近的整数。但是,如果它不是一个整数,我总是想向下舍入变量,无论它与下一个整数有多接近。有没有办法做到这一点?
回答by voidMainReturn
回答by U2EF1
One of these should work:
其中之一应该有效:
import math
math.trunc(1.5)
> 1
math.trunc(-1.5)
> -1
math.floor(1.5)
> 1
math.floor(-1.5)
> -2
回答by Makaveli
Simple
简单的
print int(x)
will work as well.
也会起作用。
回答by Makaveli
It may be very simple, but couldn't you just round it up then minus 1? For example:
这可能很简单,但你不能把它四舍五入然后减去 1 吗?例如:
number=1.5
round(number)-1
> 1
回答by Tyler
If you don't want to import math, you could use:
如果你不想导入数学,你可以使用:
int(round(x))
int(round(x))
Here's a piece of documentation:
这是一段文档:
>>> help(round)
Help on built-in function round in module __builtin__:
round(...)
round(number[, ndigits]) -> floating point number
Round a number to a given precision in decimal digits (default 0 digits).
This always returns a floating point number. Precision may be negative.
回答by Gaahbon
Don't know if you solved this, but I just stumble upon this question. If you want to get rid of decimal points, you could use int(x) and it will eliminate all decimal digits. Theres no need to use round(x).
不知道你是否解决了这个问题,但我只是偶然发现了这个问题。如果你想去掉小数点,你可以使用 int(x) ,它会消除所有的小数位。没有必要使用round(x)。
回答by DmitryG
To get floating point result simply use:
要获得浮点结果,只需使用:
round(x-0.5)
It works for negative numbers as well.
它也适用于负数。
回答by lokilindo
a lot of people say to use int(x), and this works ok for most cases, but there is a little problem. If OP's result is:
很多人说要使用int(x),这在大多数情况下都可以使用,但有一个小问题。如果 OP 的结果是:
x = 1.9999999999999999
it will round to
它将四舍五入到
x = 2
after the 16th 9 it will round. This is not a big deal if you are sure you will never come across such thing. But it's something to keep in mind.
在第 16 个 9 之后,它会圆。如果你确定你永远不会遇到这样的事情,这没什么大不了的。但有一点要记住。
回答by Mr Poin
If you working with numpy, you can use the following solution which also works with negative numbers (it's also working on arrays)
如果您使用 numpy,则可以使用以下解决方案,该解决方案也适用于负数(它也适用于数组)
import numpy as np
def round_down(num):
if num < 0:
return -np.ceil(abs(num))
else:
return np.int32(num)
round_down = np.vectorize(round_down)
round_down([-1.1, -1.5, -1.6, 0, 1.1, 1.5, 1.6])
> array([-2., -2., -2., 0., 1., 1., 1.])
I think it will also work if you just use the mathmodule instead of numpymodule.
我认为如果您只使用math模块而不是numpy模块,它也会起作用。
回答by Huy D
x//1
The //operator returns the floor of the division. Since dividing by 1 doesn't change your number, this is equivalent to floor but no import is needed.
Notes:
该//运算符返回师的地板上。由于除以 1 不会改变您的数字,这相当于 floor 但不需要导入。笔记:
- This returns a float
- This rounds towards -∞
- 这将返回一个浮点数
- 这向 -∞ 舍入

