如何使用Python将数字四舍五入到网格?

问题描述 投票:0回答:1

我想知道如何使用

python
将数字四舍五入到网格。 这是演示的伪代码:

GRID_SIZE = 20

def snap_function():
    # snap here
    pass

snapped_number = snap_function(68)
print(snapped_number)
# result is 60

不知道我给的够不够,谢谢!

python python-3.x rounding
1个回答
0
投票

难道不就是:

def snap_function(number):
    return round(number / GRID_SIZE) * GRID_SIZE

将数字缩小到每个网格单位表示为 1 的值。将此缩放后的值舍入到最接近的整数。最后,将舍入值缩放回原始网格比例。

© www.soinside.com 2019 - 2024. All rights reserved.