四舍五入到最接近的整数 [英] Round number to nearest integer

查看:115
本文介绍了四舍五入到最接近的整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试对长浮点数进行四舍五入,例如:

32.268907563;32.268907563;31.2396694215;33.6206896552;...

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

我能做什么?

代码:

for i in widthRange:对于高度范围内的 j:r, g, b = rgb_im.getpixel((i, j))h, s, v = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)高 = 高 * 360整数(轮(h))打印(小时)

解决方案

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() 获得(不是 str()!).它看起来很笨拙,但这是我找到的解决所有案例的唯一方法.它适用于 Python2 和 Python3.

def proper_round(num, dec=0):num = str(num)[:str(num).index('.')+dec+2]如果 num[-1]>='5':return float(num[:-2-(not dec)]+str(int(num[-2-(not dec)])+1))返回浮点数(数量[:-1])

测试:

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

最后,更正的答案是:

# 具有如前文所述的proper_round定义h = int(proper_round(h))

编辑 3:

测试:

<预><代码>>>>适当的圆形(6.39764125,2)6.31 # 应该是 6.4>>>适当的圆形(6.9764125,1)6.1 # 应该是 7

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

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

defproper_round(num, dec=0):num = str(num)[:str(num).index('.')+dec+2]如果 num[-1]>='5':a = num[:-2-(not dec)] # 整数部分b = int(num[-2-(not dec)])+1 # 小数部分return float(a)+b**(-dec+1) if a and b == 10 else float(a+str(b))返回浮点数(数量[:-1])

在上述情况下 b = 10 和以前的版本只会连接 ab 这将导致连接 10 后面的 0 会消失.此版本根据 decb 转换为正确的小数位,作为适当的进位.

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)

解决方案

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.

这篇关于四舍五入到最接近的整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆