A way to do this without a lot of variables and casting? Currently I have this code:

float tileX = (float)rectangle.X / (float)newTileSize;
float tileY = (float)rectangle.Y / (float)newTileSize;

int xOrigin = (int)Math.Round(tileX) * newTileSize;
int yOrigin = (int)Math.Round(tileY) * newTileSize;

// Same idea for the next 4 lines

float tileWidth = (float)rectangle.Width / (float)newTileSize;
float tileHeight = (float)rectangle.Height / (float)newTileSize;

int selectorWidth = (int)Math.Round(tileWidth) * newTileSize;
int selectorHeight = (int)Math.Round(tileHeight) * newTileSize;


The reason for the first two lines is because if I don't cast them as a float it does integer division and I was a floating point value instead so I can round it in the next two lines.

This code is fine I guess it just seems like there would be a better way to do it without so much casting. Just seems rather verbose if that makes any sense.

Thanks.
So you're looking for

int result = (int)Math.round((float)n/(float)d) * d;


(assuming you are only interested in the intermediate values for this particular calculation)

Here's a test case with an alternative:

import junit.framework.TestCase;

public class TestModulo extends TestCase {

    private final static int d = 7;

    public void testModulo() throws Exception {
        for (int n = 0; n &lt; 30; n++) {
            assertEquals("fails for " + n, f2(n), f1(n));
        }
    }

    private int f1(int n) {
        return (int) Math.round((float) n / (float) d) * d;
    }

    private int f2(int n) {
        return ((n + d / 2) / d) * d;
    }

}


You can solve your problem using only integer math.