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

16.9k Views Asked by At

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?

2

There are 2 best solutions below

3
On BEST ANSWER

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.

0
On

Use float for decimal number computations