Apologies if this was asked before, I can't seem to find an answer in regards to arbitrary multiples.
I would like a function that rounds a number by multiples. E.g, 17.4 would be rounded in multiples of 5 to 15, where 17.6 would result in 20.
This is what I came up with:
def value_2_rounded_multiple(value, multiple=1):
return round(value / multiple) * multiple
Is this reasonable, or is there a better approach?
This is some auxiliary code for testing:
multiple = 5
shift = -0.1
val_ = (multiple / 2) + shift + 3 * multiple
print("{} becomes {}".format(val_, value_2_rounded_multiple(val_, multiple=multiple)))