So I have a method in a class called Rational that passes in two ints, and I want to return a float, however this method is returning 0, and I have no idea why!
public static float DisplayFloat(int numer, int denom)
{
float result = numer / denom;
return result;
}
it is getting called like this in my main:
Console.WriteLine(Rational.DisplayFloat(12, 36));
Any ideas? I have no errors showing up in Visual Studio so I’m dumbfounded.
numeranddenomare both ints, so when you divide one by the other the result is an int ; the result is then converted to a float, but it’s too late because you already lost the fractional part. If you want a float result, cast one of the operands before doing the division: