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

如标题所示,我想取一个浮点数并将其四舍五入为最接近的整数。但是,如果它不是一个整数,那么我总是想舍入该变量,而不管它与下一个整数有多接近。有没有办法做到这一点?

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?


回答 0

简单

print int(x)

也会工作。

Simple

print int(x)

will work as well.


回答 1

其中之一应起作用:

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

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

回答 2

x//1

//运算符返回师的地板上。由于除以1不会更改您的数字,所以这等于下限,但不需要导入。笔记:

  1. 这将返回一个浮点数
  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. Notes:

  1. This returns a float
  2. This rounds towards -∞

回答 3

要获取浮点结果,只需使用:

round(x-0.5)

它也适用于负数。

To get floating point result simply use:

round(x-0.5)

It works for negative numbers as well.


回答 4

我认为您需要一个下限功能:

math.floor(x)

I think you need a floor function :

math.floor(x)


回答 5

很多人说可以使用int(x),并且在大多数情况下都可以使用,但是存在一些问题。如果OP的结果是:

x = 1.9999999999999999

它会四舍五入

x = 2

9月16日之后,它会四舍五入。如果您确定您永远不会遇到这种事情,那么这并不是什么大不了的事情。但这是要牢记的。

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:

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.


回答 6

如果您不想导入数学,则可以使用:

int(round(x))

这是一个文档:

>>> 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 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.

回答 7

如果您使用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.])

我认为如果仅使用math模块而不是numpy模块,它也将起作用。

If you working with numpy, you can use the following solution which also works with negative numbers (it’s also working on arrays)

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.


回答 8

不知道您是否解决了这个问题,但我偶然发现了这个问题。如果要去除小数点,可以使用int(x),它将消除所有十进制数字。无需使用round(x)。

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).


回答 9

只需取整(x-0.5),这将始终返回您的Float的下一个四舍五入的Integer值。您也可以通过do round(x + 0.5)轻松地四舍五入

Just make round(x-0.5) this will always return the next rounded down Integer value of your Float. You can also easily round up by do round(x+0.5)


回答 10

这可能很简单,但是您难道不可以将其舍去然后减去1吗?例如:

number=1.5
round(number)-1
> 1

It may be very simple, but couldn’t you just round it up then minus 1? For example:

number=1.5
round(number)-1
> 1

回答 11

我用此代码从数字中减去0.5,然后将其四舍五入,即原始数字四舍五入。

圆(a-0.5)

I used this code where you subtract 0.5 from the number and when you round it, it is the original number rounded down.

round(a-0.5)


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。