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 ?
You can just use
snapped = Math.round(x / step) * step
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With