Today I needed to find a quick way to programmatically determine if a given integer number (Int32) was a multiple of another integer number: as a matter of fact I needed it to build a grid-based HTML structure, but I guess that there are a ton of scenarios when such feature might be handy.
For that very reason, I've built a very simple helper class containing a couple extension methods that can be used to check if a given int (we'll call it multiple) is a multiple of another int (dividend): to determine that I've used the the modulus operator (%), which returns the remainder after dividing two numbers: if the remainder is 0, it means that multiple is evenly divisible by dividend.
Here's the extension method class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public static class IntExtensions { /// <summary> /// Returns TRUE if [multiple] is a multiple of [dividend] by dividing [multiple]/[dividend] /// and check for a remainder of zero. /// </summary> /// <param name="multiple">the number that could be a multiple of [dividend]</param> /// <param name="dividend">the number that could be a dividend of [multiple]</param> /// <returns>TRUE if [multiple] is a multiple of [dividend], FALSE otherwise.</returns> public static bool IsMultipleOf(this int multiple, int dividend) { return (multiple % dividend) == 0; } /// <summary> /// Returns TRUE if [dividend] is a multiple of [multiple] by dividing [multiple]/[dividend] /// and check for a remainder of zero. /// </summary> /// <param name="dividend">the number that could be a dividend of [multiple]</param> /// <param name="multiple">the number that could be a multiple of [dividend]</param> /// <returns>TRUE if [dividend] is a dividend of [multiple], FALSE otherwise.</returns> public static bool IsDividendOf(this int dividend, int multiple) { return multiple.IsMultipleOf(dividend); } } |
As we can see, there are two methods that are based upon the same logic:
- IsMultipleOf(), which returns TRUE if the former int is a multiple of the latter, FALSE otherwise.
- IsDividendOf(), which is basically the reciprocal of the previous one, which returns TRUE if the former int is a dividend of the latter, FALSE otherwise.
If you don't know how to use the above Extension Methods in a real-case scenario, read this article.
In case you don't like Extension Methods, here's the same code written as a static helper class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public static class IntUtil { /// <summary> /// Returns TRUE if [multiple] is a multiple of [dividend] by dividing [multiple]/[dividend] and check for a remainder of zero. /// </summary> /// <param name="multiple">the number that could be a multiple of [dividend]</param> /// <param name="dividend">the number that could be a dividend of [multiple]</param> /// <returns>TRUE if [multiple] is a multiple of [dividend], FALSE otherwise.</returns> public static bool IsMultipleOf(int multiple, int dividend) { return (multiple % dividend) == 0; } /// <summary> /// Returns TRUE if [dividend] is a multiple of [multiple] by dividing [multiple]/[dividend] and check for a remainder of zero. /// </summary> /// <param name="dividend">the number that could be a dividend of [multiple]</param> /// <param name="multiple">the number that could be a multiple of [dividend]</param> /// <returns>TRUE if [dividend] is a dividend of [multiple], FALSE otherwise.</returns> public static bool IsDividendOf(int dividend, int multiple) { return IsMultipleOf(multiple, dividend); } } |
Conclusions
That's all: I hope that this minimalistic set of extension methods will help other developers looking for a quick and effective way to check if a number is a number (or a dividend) or another.