Noob Question.
double answer = 13/5;
System.out.println(answer);
Why does that return 2 instead of 2.6.
As in must i say (double) 13/5 everytime i want to print a double.
Noob Question.
double answer = 13/5;
System.out.println(answer);
Why does that return 2 instead of 2.6.
As in must i say (double) 13/5 everytime i want to print a double.
you must tell java that you want a double division:
double answer = 13/5.0; // or 13 / (double) 5.0
13 and 5 are integers. That means that you divide them as integers. As a result, you will get an integer. So what you have to do is to cast them as double like that:
double answer = 13 / (double) 5;
Or write them like that:
double answer = 13 / 5.0;
Because 13
and 5
are integers, so the result will be an integer that you are assigning to a double
variable
You need to specify your numbers should be considered as double
The division takes place between two int
s and the result is also an int
which is finally cast into a double
. You should declare the operands as double
s to get the desired result.