To snap a variable, you can use this:
var snapped_x = grid_size * integer(nonsnapped_x / grid_size);


Visit my site: www.masterq32.de