-5
public static double Fraction2Decimal(int Numerator, int Denominator)
{
    double Answer = Numerator / Denominator;
    return Answer;
}

public static void main(String args[])
{
    double ans = Fraction2Decimal(2, 8);

    double pi = 3.141592653589793238462643383;
    System.out.print(ans);
}

What this code is doing is converting fractions to a decimal/double, 2/8 as a decimal is 0.25 as everyone knows but IntelliJ IDE returns this

0.0
Process finished with exit code 0

Huh! its should return 0.25, could anyone explain what is going on here?

EDIT: I only did by just to check that the type was to short for the answer.

1 Answers1

-1

Because you are dividing to ints, that can only produce an int. And on division, they get floored (rounded to the lower integer). Try casting them to double o float before doing the operation.

Myxoh
  • 73
  • 8