title: How to always round down to the nearest multiple of X in Javascript?
body: <p>I have a grid drawn on a canvas, when the user clicks on the grid, I am drawing a rectangle. I want to always draw the rectangle on top of the grid cell the user is clicking on. So I need to round down to the nearest X, in my case, a multiple of 40.</p>
  
  <p>Some examples ...</p>
  
  <pre><code>121 =&gt; 120
  125 =&gt; 120
  139 =&gt; 120
  159 =&gt; 120
  160 =&gt; 160
  </code></pre>
  
  <p>So far I have the rounding working using the following...</p>
  
  <pre><code>x = Math.round(x / constants.MAP_CELL_SIZE) * constants.MAP_CELL_SIZE;
  </code></pre>
  
  <p>This <strong>almost</strong> handles the rounding, the only thing I am missing is the rounding down to the nearest multiple of 40, which is held in <code>constants.MAP_CELL_SIZE</code>.</p>
  
  <p>Hopefully this makes sense, and someone can lend a hand ... much appreciated!</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>It was just as simple as switching from <code>Math.round</code> to <code>Math.floor</code>.</p>