This question already has answers here:
Why double width = 50/110000; the output is 0.000000000000000?
(3 answers)
Closed 9 years ago.
double overallMark = ((20/100) * homeworkAverage) + ((80/100) * examinationAverage);
Is something wrong with my syntax? I'm getting 0.0 as an answer :(
I need to add 20% of homeworkAverage to 80% of examinationAverage!
You are performing integer division with 20/100, and in Java, dividing ints yields an int, the truncated quotient.
Cast one of them as a double or use double literals to force floating-point division:
((double) 20 / 100)
or
(20.0 / 100.0)
... and similarly you'll need to change (80/100).
20/100 becomes 0 because of integer division.
Try changing it to 20.0/100.0 so that doubles are used instead.
Related
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I am using Math.round(). This is the sample code:
double value = 0.14499999970197677;
value = Math.round( value* 100.0) / 100.0;
My expectation is it should return 0.15 but it is returning 0.14
Also, if the value is 0.13499999970197677, then it is returning 0.13, why not 0.14
I have already gone through the link round up to 2 decimal places in java?
Please tell me clearly that why this is happening with both the numbers?
In Java, Math.round rounds up/down to closest long value (in mathematics terms, closest whole number).
14.49 will be rounded to 14 (because it is treated as 14.4). It won't take the .09 into account.
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.
This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 7 years ago.
I used the following code:
double pow = 3/7;
double num = 85;
System.out.println(Math.pow(num, pow));
Expected result:
6.71...
The output is
1.0
Any idea why?
3/7 is evaluated to 0, since you are dividing two integers, so Math.pow(num, pow) becomes Math.pow(num, 0.0) which is 1.0.
Change it to 3.0/7 in order to get a floating point result.
This question already has answers here:
Double division behaving wrongly
(4 answers)
Closed 7 years ago.
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 ints and the result is also an int which is finally cast into a double. You should declare the operands as doubles to get the desired result.
This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Why does this Java division print out zero? [duplicate]
(5 answers)
Closed 8 years ago.
I am using Java 1.6
final double check = 3 / 4;
System.out.println(check);
Console is showing: 0.0
Why is this happening? Shouldn't it come out 0.75?
Make that:
double check = 3.0 / 4;
and it'll work. You got 0 because 3 / 4 is an integer division, whose value is 0.
Because both are integer hence result will also be integer.
Cast any one into double like this:
double check = (double)3 / 4;
By doing:
3 / 4
you are performing an integer division, since 3 and 4 are int constants, not doubles. The result is therefore an int. But since you are assigning it to a double, the result of the division will then be promoted to a double. But at this point it is too late, since the integer division will have produced 0!
You need to do:
3.0 / 4
to achieve your desired result, since in this case, 4 will automatically be promoted to a double, and the result of the division will also be a double.
To be perfectly sure of what happens and if you like symmetry, you can also write:
3.0 / 4.0
You are dividing integers and assigning the result to double.In java division of two int values always yields an int.
So change the statement double check = 3 / 4; to double check = 3.0 / 4;