# Check if a divided variable returns an integer

Hey!

I want to check if a variable divided by five returns an integer. I have this:

``````if ((i/5) == "an integer" )
``````

Appretiate all help, thank you!

Supposing `i` is an integer, then `(i/5)` will always return an integer (even if i = 4 for example)

When you divide two integers, the result is always an integer. For example, the result of 7 / 3 is 2

If `i` is a float, then `(i/5)` will always return a float (even if i = 5.0f)

To obtain a quotient as a rational number or fraction, give the dividend or divisor type float or type double

I advise you to check if `(i % 5) == 0`, meaning that the remainder after division of i by 5 gives you 0, meaning that `i` is a multiple of 5

if ((i/5) == Mathf.RoundToInt(i/5)) {… }

If i is an integer variable, then i/5 will also be an integer - i.e. any remainder bits will be chopped off, because integer variables can only store whole numbers.

If i is an integer and what you actually want to know is “is i divisible by 5” then use the modulus (aka remainder) integer operator:

``````//if the remainder of i/5 is 0...
if((i%5)==0)
{
}
``````

If i is a float, then the result may well contain a fractional part. Floating point values are not precise, so you can’t easily check if the division was exactly an integer result. However you can get the fractional part and check if it is approximately 0:

``````//do your division
float my_division = i / 5.0f;

//mathf.Round gets the closest whole number, so by subtracting round(my_division)
//from my_division we are left with the fractional part
float remainder = my_division - Mathf.Round(my_division);

//now we can check if the remainder is approximately 0
if(Mathf.Approximately(remainder,0))
{
}
``````

That’s the long winded way to write it! We could get it on fewer lines though:

``````    //or all on fewer lines...
float my_division = i/5;
if(Mathf.Approximately(my_division-Mathf.Round(my_division),0))
{
}
``````

Or we could even put it in a function…

``````bool IsAnInteger(float val)
{
return Mathf.Approximately(val-Mathf.Round(val),0);
}

bool IsDivisibleBy(float a, float b)
{
return IsAnInteger(a/b);
}
``````