Search code examples
javadrjava

How to get the full fraction value of a division result in java?


I am new to Java and I am using DrJava IDE for my testing. I have the following division 49700/40000 and it displays 1.0 instead of 1.2425.

 double t = 49700/40000;
 System.out.println(t);

Is it something I am doing wrong?


Solution

  • Try, this instead:

    double t = 49700/40000.0;
    System.out.println(t);
    

    If both operands are integers the result will be an integer which will be truncated, and then it will be cast in to a double. If, instead, one of the operands is a double, the result will be a double.