当前位置: 首页 > 面试题库 >

将数字舍入到最接近的整数

凌清夷
2023-03-14
问题内容

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

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)

问题答案:

int(round(x))

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

编辑:

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

h = int(round(h))

将新值分配给h

编辑2:

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

round()似乎无法正确舍入

避免此问题的一种方法是使用此答案所述的十进制:

如果使用小数模块,则无需使用“舍入”功能就可以近似。这是我用于舍入的内容,尤其是在编写货币应用程序时:

Decimal(str(16.2)).quantize(Decimal('.01'), rounding=ROUND_UP)

这将返回一个十进制数为16.20。

为了使此答案在不使用额外库的情况下正常运行,使用自定义舍入函数会很方便。经过大量的更正之后,我想出了以下解决方案,据我测试避免了所有存储问题。它基于使用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



 类似资料:
  • 问题内容: 我正在尝试在python中舍入整数。我看了内置的round()函数,但似乎舍入浮动了。 我的目标是将整数四舍五入到最接近的10的倍数。即:5-> 10、4-> 0、95-> 100,等等。 5和更高的值应四舍五入,4和更低的值应四舍五入。 这是我这样做的代码: 这是实现我想要实现的最好方法吗?有内置的功能吗?另外,如果这是最好的方法,那么我在测试中遗漏的代码是否有问题? 问题答案: 实

  • 问题内容: 我需要以下结果 还是?我该怎么办? 问题答案: 您可以使用将小数位数减少为零。假设拥有要取整的值: 使用起来有点麻烦,因为它要求您指定要保留的位数。在您的示例中,该值为3,但这不适用于所有值: (请注意,对象是不可改变的,都和会返回一个新的对象。)

  • 问题内容: 我们如何在php中将数字四舍五入到最接近的10? 说我有,我将使用什么代码将其四舍五入? 问题答案: 会下降。 会上升。 默认情况下会最接近。 除以10,得到ceil,然后乘以10以减少有效数字。 编辑:我已经这样做了很长时间..但是TallGreenTree的答案是更干净。

  • 问题内容: 我想将a舍入到最接近的10的倍数。 例如,如果数字为8.0,则四舍五入为10。如果数字为2.0,则将其四舍五入为0。 我怎样才能做到这一点? 问题答案: 您可以使用函数(将浮点数四舍五入到最接近的整数值)并应用“比例因子” 10: 用法示例: 在第二个示例中,除以十(),四舍五入(),转换为整数(),然后再乘以10()。 斯威夫特3: 或者:

  • 问题内容: 我可以在python中进行以下舍入: 四舍五入到最接近的05小数 7,97-> 7,95 6,72-> 6,70 31,06-> 31,05 36,04-> 36,05 5,25-> 5,25 希望有道理。 问题答案:

  • 问题内容: 我不确定是否曾经问过这个问题,但是如何在T-SQL中将平均值四舍五入到最接近的整数? 问题答案: 这为它工作: 难道没有更短的方法吗?