我一直在try 对长浮点数进行舍入,如下所示:

32.268907563;
32.268907563;
31.2396694215;
33.6206896552;
...

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

我能做什么?

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)

推荐答案

TL;DR:

round(x)

将对其进行四舍五入并将其更改为整数.

您没有给任何变量赋值round(h).当你调用round(h)时,它返回整数,但不做其他任何操作;您必须将该行更改为:

h = round(h)

将新值指定给h.


正如@plowman在 comments 中所说的,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位小数可以是9,如果第dec+1位>=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相关问答推荐

如何从比较函数生成ngroup?

如何在验证文本列表时使正则表达式无序?

如何在Airflow执行日期中保留日期并将时间转换为00:00

ModuleNotFoundError:Python中没有名为google的模块''

Python OPCUA,modbus通信代码运行3小时后出现RuntimeError

高效生成累积式三角矩阵

如何在SQLAlchemy + Alembic中定义一个"Index()",在基表中的列上

为什么在生成时间序列时,元组索引会超出范围?

Numpy`astype(Int)`给出`np.int64`而不是`int`-怎么办?

解析CSV文件以将详细信息添加到XML文件

Pandas:根据相邻行之间的差异过滤数据帧

生产者/消费者-Queue.get by list

Networkx中K-Shell最核心的 node

Python-迭代PANAS中的数据框并替换列表中不包含字符串的值

如何从多个词典中制作Pandas 数据帧?

在动态创建带有图像的按钮时遇到问题

如何验证像这样添加的对象属性:MyObj.newattribute=123

使用子进程的Imagemagick转换函数,从bytesio输入图像

如何编写拆分和 Select 每个PANAS列中的第一个元素的Python函数

基于多个条件和未知数的数据抽取算法