Last active
October 24, 2020 21:28
-
-
Save mattlundstrom/bcfe5cf14f7ae965ce2d27aa68662991 to your computer and use it in GitHub Desktop.
JS Snap a number to an increment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
val = value to snap | |
inc = increment to snap to | |
example: | |
snapToIncrement(10, 3): 9 | |
snapToIncrement(89, 10): 100 | |
snapToIncrement(35.6, .5): 35.5 | |
*/ | |
function snapToIncrement( val, inc ){ | |
return Math.round(val / increment) * increment; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment