简体   繁体   English

将浮点数四舍五入到最接近的整数?

[英]Round a floating-point number down to the nearest integer?

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?有没有办法做到这一点?

Simple简单的

int(x)

will work as well.也会起作用。

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
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.由于除以 1 不会改变你的数字,这相当于 floor 但不需要导入。 Notes:笔记:

  1. This returns a float这将返回一个浮点数
  2. This rounds towards -∞这向 -∞ 取整

To get floating point result simply use:要获得浮点结果,只需使用:

round(x-0.5)

It works for negative numbers as well.它也适用于负数。

I think you need a floor function :我认为您需要一个楼层功能:

math.floor(x)数学.地板(x)

a lot of people say to use int(x) , and this works ok for most cases, but there is a little problem.很多人说要使用int(x) ,这在大多数情况下都可以,但是有一个小问题。 If OP's result is:如果 OP 的结果是:

x = 1.9999999999999999

it will round to它会四舍五入

x = 2

after the 16th 9 it will round. 16 号 9 号之后,它会圆整。 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.但这是要记住的事情。

If you don't want to import math, you could use:如果您不想导入数学,可以使用:

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.

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 math module instead of numpy module.我认为如果您只使用math模块而不是numpy模块,它也会起作用。

Just make round(x-0.5) this will always return the next rounded down Integer value of your Float.只需进行 round(x-0.5) 这将始终返回浮点的下一个向下舍入的整数值。 You can also easily round up by do round(x+0.5)您也可以通过 do round(x+0.5) 轻松四舍五入

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.如果你想去掉小数点,你可以使用 int(x) 它将消除所有小数位。 Theres no need to use round(x).无需使用round(x)。

It may be very simple, but couldn't you just round it up then minus 1?这可能很简单,但你不能把它四舍五入然后减去 1 吗? For example:例如:

number=1.5
round(number)-1
> 1

I used this code where you subtract 0.5 from the number and when you round it, it is the original number rounded down.我用这个代码从数字中减去 0.5,当你四舍五入时,它是向下舍入的原始数字。

round(a-0.5)圆形(a-0.5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM