Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closest value (snapping)

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 ?

like image 662
katie Avatar asked Dec 19 '22 00:12

katie


1 Answers

You can just use

snapped = Math.round(x / step) * step
like image 146
6502 Avatar answered Dec 24 '22 02:12

6502