Skip to content

Instantly share code, notes, and snippets.

@mattlundstrom
Last active October 24, 2020 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattlundstrom/bcfe5cf14f7ae965ce2d27aa68662991 to your computer and use it in GitHub Desktop.
Save mattlundstrom/bcfe5cf14f7ae965ce2d27aa68662991 to your computer and use it in GitHub Desktop.
JS Snap a number to an increment
/*
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