问题:将数字舍入到最接近的整数

我一直试图舍入长浮点数,例如:

32.268907563;
32.268907563;
31.2396694215;
33.6206896552;
...

到目前为止没有成功。我想math.ceil(x)math.floor(x)(尽管这或圆形上下,这是不是我要找的)和round(x)它没有任何工作(还是浮点数)。

我能做什么?

编辑:代码:

for i in widthRange:
    for j in heightRange:
        r, g, b = rgb_im.getpixel((i, j))
        h, s, v = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
        h = h * 360
        int(round(h))
        print(h)

I’ve been trying to round long float numbers like:

32.268907563;
32.268907563;
31.2396694215;
33.6206896552;
...

With no success so far. I tried math.ceil(x), math.floor(x) (although that would round up or down, which is not what I’m looking for) and round(x) which didn’t work either (still float numbers).

What could I do?

EDIT: CODE:

for i in widthRange:
    for j in heightRange:
        r, g, b = rgb_im.getpixel((i, j))
        h, s, v = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
        h = h * 360
        int(round(h))
        print(h)

回答 0

int(round(x))

将其舍入并将其更改为整数

编辑:

您没有将int(round(h))分配给任何变量。当您调用int(round(h))时,它返回整数,但不执行其他任何操作。您必须将该行更改为:

h = int(round(h))

将新值分配给h

编辑2:

就像@plowman在评论中说的那样,Python round()无法正常运行,这是因为数字作为变量存储的方式通常不是您在屏幕上看到的方式。有很多答案可以解释此行为:

round()似乎无法正确舍入

避免此问题的一种方法是使用此答案所述的十进制:https : //stackoverflow.com/a/15398691/4345659

为了使此答案正确运行而不使用额外的库,使用自定义舍入函数会很方便。经过大量的更正之后,我想出了以下解决方案,据我测试避免了所有存储问题。它基于使用通过repr()(NOT str()!)获得的字符串表示形式。它看起来很黑,但这是我发现解决所有问题的唯一方法。它同时适用于Python2和Python3。

def proper_round(num, dec=0):
    num = str(num)[:str(num).index('.')+dec+2]
    if num[-1]>='5':
        return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
    return float(num[:-1])

测试:

>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>> 
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0

最后,正确的答案将是:

# Having proper_round defined as previously stated
h = int(proper_round(h))

编辑3:

测试:

>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1  # should be 7

此处的陷阱是,dec第-小数位数可以为9,如果dec+1-th位数> = 5,则9将变为0,并且应将1携带至dec-1第-位数。

如果考虑到这一点,我们将得到:

def proper_round(num, dec=0):
    num = str(num)[:str(num).index('.')+dec+2]
    if num[-1]>='5':
      a = num[:-2-(not dec)]       # integer part
      b = int(num[-2-(not dec)])+1 # decimal part
      return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
    return float(num[:-1])

在上述情况下b = 10,以前的版本将串联在一起ab这将导致10尾随0消失的位置的串联。此版本b会根据适当地转换为右小数位dec

int(round(x))

Will round it and change it to integer

EDIT:

You are not assigning int(round(h)) to any variable. When you call int(round(h)), it returns the integer number but does nothing else; you have to change that line for:

h = int(round(h))

To assign the new value to h

EDIT 2:

As @plowman said in the comments, Python’s round() doesn’t work as one would normally expect, and that’s because the way the number is stored as a variable is usually not the way you see it on screen. There are lots of answers that explain this behavior:

round() doesn’t seem to be rounding properly

One way to avoid this problem is to use the Decimal as stated by this answer: https://stackoverflow.com/a/15398691/4345659

In order for this answer to work properly without using extra libraries it would be convenient to use a custom rounding function. After a lot of corrections, I came up with the following solution, that as far as I tested avoided all the storing issues. It is based on using the string representation, obtained with repr() (NOT str()!). It looks hacky but it was the only way I found to solve all the cases. It works with both Python2 and Python3.

def proper_round(num, dec=0):
    num = str(num)[:str(num).index('.')+dec+2]
    if num[-1]>='5':
        return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))
    return float(num[:-1])

Tests:

>>> print(proper_round(1.0005,3))
1.001
>>> print(proper_round(2.0005,3))
2.001
>>> print(proper_round(3.0005,3))
3.001
>>> print(proper_round(4.0005,3))
4.001
>>> print(proper_round(5.0005,3))
5.001
>>> print(proper_round(1.005,2))
1.01
>>> print(proper_round(2.005,2))
2.01
>>> print(proper_round(3.005,2))
3.01
>>> print(proper_round(4.005,2))
4.01
>>> print(proper_round(5.005,2))
5.01
>>> print(proper_round(1.05,1))
1.1
>>> print(proper_round(2.05,1))
2.1
>>> print(proper_round(3.05,1))
3.1
>>> print(proper_round(4.05,1))
4.1
>>> print(proper_round(5.05,1))
5.1
>>> print(proper_round(1.5))
2.0
>>> print(proper_round(2.5))
3.0
>>> print(proper_round(3.5))
4.0
>>> print(proper_round(4.5))
5.0
>>> print(proper_round(5.5))
6.0
>>> 
>>> print(proper_round(1.000499999999,3))
1.0
>>> print(proper_round(2.000499999999,3))
2.0
>>> print(proper_round(3.000499999999,3))
3.0
>>> print(proper_round(4.000499999999,3))
4.0
>>> print(proper_round(5.000499999999,3))
5.0
>>> print(proper_round(1.00499999999,2))
1.0
>>> print(proper_round(2.00499999999,2))
2.0
>>> print(proper_round(3.00499999999,2))
3.0
>>> print(proper_round(4.00499999999,2))
4.0
>>> print(proper_round(5.00499999999,2))
5.0
>>> print(proper_round(1.0499999999,1))
1.0
>>> print(proper_round(2.0499999999,1))
2.0
>>> print(proper_round(3.0499999999,1))
3.0
>>> print(proper_round(4.0499999999,1))
4.0
>>> print(proper_round(5.0499999999,1))
5.0
>>> print(proper_round(1.499999999))
1.0
>>> print(proper_round(2.499999999))
2.0
>>> print(proper_round(3.499999999))
3.0
>>> print(proper_round(4.499999999))
4.0
>>> print(proper_round(5.499999999))
5.0

Finally, the corrected answer would be:

# Having proper_round defined as previously stated
h = int(proper_round(h))

EDIT 3:

Tests:

>>> proper_round(6.39764125, 2)
6.31 # should be 6.4
>>> proper_round(6.9764125, 1)
6.1  # should be 7

The gotcha here is that the dec-th decimal can be 9 and if the dec+1-th digit >=5 the 9 will become a 0 and a 1 should be carried to the dec-1-th digit.

If we take this into consideration, we get:

def proper_round(num, dec=0):
    num = str(num)[:str(num).index('.')+dec+2]
    if num[-1]>='5':
      a = num[:-2-(not dec)]       # integer part
      b = int(num[-2-(not dec)])+1 # decimal part
      return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))
    return float(num[:-1])

In the situation described above b = 10 and the previous version would just concatenate a and b which would result in a concatenation of 10 where the trailing 0 would disappear. This version transforms b to the right decimal place based on dec, as a proper carry.


回答 1

使用round(x, y)。它将把您的数字四舍五入到所需的小数位。

例如:

>>> round(32.268907563, 3)
32.269

Use round(x, y). It will round up your number up to your desired decimal place.

For example:

>>> round(32.268907563, 3)
32.269

回答 2

round(value,significantDigit)是普通的解决方案,但是,当舍入值以结束时,这并不像从数学角度所期望的那样起作用5。如果5在四舍五入到的数字后面紧跟着数字,则有时仅按预期将这些值四舍五入(即8.005四舍五入为两位小数8.01)。对于某些由于浮点数学的怪异而导致的值,它们会四舍五入!

>>> round(1.0005,3)
1.0
>>> round(2.0005,3)
2.001
>>> round(3.0005,3)
3.001
>>> round(4.0005,3)
4.0
>>> round(1.005,2)
1.0
>>> round(5.005,2)
5.0
>>> round(6.005,2)
6.0
>>> round(7.005,2)
7.0
>>> round(3.005,2)
3.0
>>> round(8.005,2)
8.01

奇怪的。

假设您的意图是对科学中的统计数据进行传统的四舍五入,这是一个方便的包装方法,可以使round函数按预期工作,并且需要import额外的功能,例如Decimal

>>> round(0.075,2)

0.07

>>> round(0.075+10**(-2*5),2)

0.08

啊哈!因此,基于此我们可以创建一个函数…

def roundTraditional(val,digits):
   return round(val+10**(-len(str(val))-1), digits)

基本上,这会增加一个值,该值保证小于您要在其上使用的字符串的最小给定数字round。通过添加少量它保留的round在大多数情况下的行为,而现在如果确保数字逊于一个是四舍五入到是5它四舍五入,如果是4它几轮下来。

使用该方法的目的10**(-len(val)-1)是故意的,因为它是您可以添加的最大的小数字以强制移位,同时还要确保所添加的值不会改变舍入,即使.缺少小数点也是如此。我可以使用10**(-len(val))条件if (val>1)减法来减去1更多…但是总要减去法则比较简单,1因为这不会改变此解决方法可以正确处理的十进制数的适用范围。如果您的值达到该类型的限制,则此方法将失败,但将失败,但是对于几乎所有有效十进制值的范围,它都应起作用。

您也可以使用十进制库来完成此操作,但是我建议的包装器更简单,在某些情况下可能更受欢迎。


编辑:感谢Blckknght指出,5条纹情况只发生在某些值。另外,此答案的较早版本还不够明确,仅当紧邻要舍入的数字的下标为时才发生5奇数舍入行为。

round(value,significantDigit) is the ordinary solution, however this does not operate as one would expect from a math perspective when round values ending in 5. If the 5 is in the digit just after the one you’re rounded to, these values are only sometimes rounded up as expected (i.e. 8.005 rounding to two decimal digits gives 8.01). For certain values due to the quirks of floating point math, they are rounded down instead!

i.e.

>>> round(1.0005,3)
1.0
>>> round(2.0005,3)
2.001
>>> round(3.0005,3)
3.001
>>> round(4.0005,3)
4.0
>>> round(1.005,2)
1.0
>>> round(5.005,2)
5.0
>>> round(6.005,2)
6.0
>>> round(7.005,2)
7.0
>>> round(3.005,2)
3.0
>>> round(8.005,2)
8.01

Weird.

Assuming your intent is to do the traditional rounding for statistics in the sciences, this is a handy wrapper to get the round function working as expected needing to import extra stuff like Decimal.

>>> round(0.075,2)

0.07

>>> round(0.075+10**(-2*5),2)

0.08

Aha! So based on this we can make a function…

def roundTraditional(val,digits):
   return round(val+10**(-len(str(val))-1), digits)

Basically this adds a value guaranteed to be smaller than the least given digit of the string you’re trying to use round on. By adding that small quantity it preserve’s round‘s behavior in most cases, while now ensuring if the digit inferior to the one being rounded to is 5 it rounds up, and if it is 4 it rounds down.

The approach of using 10**(-len(val)-1) was deliberate, as it the largest small number you can add to force the shift, while also ensuring that the value you add never changes the rounding even if the decimal . is missing. I could use just 10**(-len(val)) with a condiditional if (val>1) to subtract 1 more… but it’s simpler to just always subtract the 1 as that won’t change much the applicable range of decimal numbers this workaround can properly handle. This approach will fail if your values reaches the limits of the type, this will fail, but for nearly the entire range of valid decimal values it should work.

You can also use the decimal library to accomplish this, but the wrapper I propose is simpler and may be preferred in some cases.


Edit: Thanks Blckknght for pointing out that the 5 fringe case occurs only for certain values. Also an earlier version of this answer wasn’t explicit enough that the odd rounding behavior occurs only when the digit immediately inferior to the digit you’re rounding to has a 5.


回答 3

对于正面,请尝试

int(x + 0.5)

要使其也适用于底片,请尝试

int(x + (0.5 if x > 0 else -0.5))

int()就像下层函数一样工作,因此您可以利用此属性。这绝对是最快的方法。

For positives, try

int(x + 0.5)

To make it work for negatives too, try

int(x + (0.5 if x > 0 else -0.5))

int() works like a floor function and hence you can exploit this property. This is definitely the fastest way.


回答 4

按照IEEE 754的规定,Python不仅会做一半甚至一半的运算吗?

小心重新定义或使用“非标准”舍入…

(另请参阅https://stackoverflow.com/a/33019948/109839

Isn’t just Python doing round half to even, as prescribed by IEEE 754?

Be careful redefining, or using “non-standard” rounding…

(See also https://stackoverflow.com/a/33019948/109839)


回答 5

您也可以使用numpy,假设您正在使用python3.x,这是一个示例

import numpy as np
x = 2.3
print(np.rint(x))
>>> 2.0

You can also use numpy assuming if you are using python3.x here is an example

import numpy as np
x = 2.3
print(np.rint(x))
>>> 2.0

回答 6

您的解决方案是在不指定第二个参数(小数位数)的情况下调用round

>>> round(0.44)
0
>>> round(0.64)
1

比起更好的结果

>>> int(round(0.44, 2))
0
>>> int(round(0.64, 2))
0

来自Python文档,网址为 https://docs.python.org/3/library/functions.html#round

舍入(数字[,n位数字])

返回数字舍入到小数点后的n位精度。如果省略ndigits或为None,则返回与其输入最接近的整数。

注意

针对float的round()行为可能令人惊讶:例如,round(2.675,2)给出2.67,而不是预期的2.68。这不是错误:这是由于大多数十进制小数不能完全表示为浮点数的结果。有关更多信息,请参见浮点算法:问题和限制。

Your solution is calling round without specifying the second argument (number of decimal places)

>>> round(0.44)
0
>>> round(0.64)
1

which is a much better result than

>>> int(round(0.44, 2))
0
>>> int(round(0.64, 2))
0

From the Python documentation at https://docs.python.org/3/library/functions.html#round

round(number[, ndigits])

Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.

Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.


回答 7

如果您需要(例如)A的两位数近似值,则 int(A*100+0.5)/100.0则将完成您要查找的操作。

如果需要三位数的近似值,请乘以1000除以此类推。

If you need (for example) a two digit approximation for A, then int(A*100+0.5)/100.0 will do what you are looking for.

If you need three digit approximation multiply and divide by 1000 and so on.


回答 8

这样的事情也应该起作用

import numpy as np    

def proper_round(a):
    '''
    given any real number 'a' returns an integer closest to 'a'
    '''
    a_ceil = np.ceil(a)
    a_floor = np.floor(a)
    if np.abs(a_ceil - a) < np.abs(a_floor - a):
        return int(a_ceil)
    else:
        return int(a_floor)

Some thing like this should also work

import numpy as np    

def proper_round(a):
    '''
    given any real number 'a' returns an integer closest to 'a'
    '''
    a_ceil = np.ceil(a)
    a_floor = np.floor(a)
    if np.abs(a_ceil - a) < np.abs(a_floor - a):
        return int(a_ceil)
    else:
        return int(a_floor)

回答 9

为此,我建议您做以下事情-

int(round(x))

这将为您提供最接近的整数。

希望这可以帮助!!

For this purpose I would suggest just do the following thing –

int(round(x))

This will give you nearest integer.

Hope this helps!!


回答 10

我使用并建议以下解决方案(python3.6):

y = int(x + (x % (1 if x >= 0 else -1)))

它适用于半数(正数和负数),甚至比int(round(x))更快:

round_methods = [lambda x: int(round(x)), 
                 lambda x: int(x + (x % (1 if x >= 0 else -1))),
                 lambda x: np.rint(x).astype(int),
                 lambda x: int(proper_round(x))]

for rm in round_methods:
    %timeit rm(112.5)
Out:
201 ns ± 3.96 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
159 ns ± 0.646 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
925 ns ± 7.66 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.18 µs ± 8.66 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

for rm in round_methods:
    print(rm(112.4), rm(112.5), rm(112.6))
    print(rm(-12.4), rm(-12.5), rm(-12.6))
    print('=' * 11)

Out:
112 112 113
-12 -12 -13
===========
112 113 113
-12 -13 -13
===========
112 112 113
-12 -12 -13
===========
112 113 113
-12 -13 -13
===========

I use and may advise the following solution (python3.6):

y = int(x + (x % (1 if x >= 0 else -1)))

It works fine for half-numbers (positives and negatives) and works even faster than int(round(x)):

round_methods = [lambda x: int(round(x)), 
                 lambda x: int(x + (x % (1 if x >= 0 else -1))),
                 lambda x: np.rint(x).astype(int),
                 lambda x: int(proper_round(x))]

for rm in round_methods:
    %timeit rm(112.5)
Out:
201 ns ± 3.96 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
159 ns ± 0.646 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
925 ns ± 7.66 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.18 µs ± 8.66 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

for rm in round_methods:
    print(rm(112.4), rm(112.5), rm(112.6))
    print(rm(-12.4), rm(-12.5), rm(-12.6))
    print('=' * 11)

Out:
112 112 113
-12 -12 -13
===========
112 113 113
-12 -13 -13
===========
112 112 113
-12 -12 -13
===========
112 113 113
-12 -13 -13
===========

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