if I have a range slider that uses steps, for example from 0 to 100 with a step of 5, and then I have a random value between that range, let's say 56, how can I determine what's the closest snap value (55) ?
I was thinking to loop forwards and backwards until I find the closest number divisible by 5, but there has to be a better approach ?
if I have a range slider that uses steps, for example from 0 to 100 with a step of 5, and then I have a random value between that range, let's say 56, how can I determine what's the closest snap value (55) ?
I was thinking to loop forwards and backwards until I find the closest number divisible by 5, but there has to be a better approach ?
Share Improve this question asked Aug 29, 2015 at 6:58 katiekatie 1,0512 gold badges12 silver badges19 bronze badges 01 Answer
Reset to default 19You can just use
snapped = Math.round(x / step) * step