我想知道如何使用
python
将数字四舍五入到网格。
这是演示的伪代码:
GRID_SIZE = 20
def snap_function():
# snap here
pass
snapped_number = snap_function(68)
print(snapped_number)
# result is 60
不知道我给的够不够,谢谢!
难道不就是:
def snap_function(number):
return round(number / GRID_SIZE) * GRID_SIZE
将数字缩小到每个网格单位表示为 1 的值。将此缩放后的值舍入到最接近的整数。最后,将舍入值缩放回原始网格比例。