python - 将数字四舍五入到最接近的整数

标签 python

我一直在尝试四舍五入长 float ,例如:

32.268907563;
32.268907563;
31.2396694215;
33.6206896552;
...

到目前为止没有成功。我尝试了 math.ceil(x)math.floor(x) (尽管这会向上或向下舍入,这不是我想要的)和 round(x) 也不起作用(仍然是 float )。

我能做什么?

代码:

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)

最佳答案

TL;DR:

round(x)

将其四舍五入并将其更改为整数。

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

h = round(h)

将新值分配给 h


正如@plowman 在评论中所说,Python 的 round() 不能像人们通常期望的那样工作,这是因为数字作为变量存储的方式通常不是你的方式在屏幕上看到它。有 lots of answers 可以解释这种行为。

避免此问题的一种方法是使用 this answer 所述的小数。

为了让这个答案在不使用额外库的情况下正常工作,使用自定义舍入函数会很方便。我想出了以下解决方案,据我测试,它避免了所有存储问题。它基于使用 repr() 获得的字符串表示(不是 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))

测试:

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

这里的问题是第 dec-th 十进制数可以是 9,如果 dec+1-th 数字 >=5,则 9 将变为 0 和1 应该被带到 dec-1-th 数字。

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

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 会消失。此版本根据 decb 转换为正确的小数位,作为正确的进位。

关于python - 将数字四舍五入到最接近的整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31818050/

相关文章:

Python Tkinter : obtain tree node information

python - 获取昨天的日期和时区

python - 如何将Python版本降级到Python 2.7.13(默认,2017年11月7日,00 :42:48)?

Python:使用 ElementTree 访问 XML 中的下一个元素

python - TensorFlow keras 模型 fit() 参数 steps_per_epoch 和 epochs 在训练集上的行为

python - 如何从图像创建 4D numpy 数组?

python - 我如何过滤 itertools chain() 结果?

python - 放置和删除 Django

python - 使用 Regex 和 Pandas 格式化问题

Python:如何将一个数组中的元素与另一个数组中的列/行相乘