+ 1
Guysss... I'm not getting the given answer of this question. What should i do....??? Can anyone help me.
7 Antworten
+ 1
What is the task description? Because 8/100 * 20 is 1.6, not 0.96.
Also remember that the return type double does not make the calculation a floating point one if all numbers involved in the calculation are integers. 8/100 is zero in integer division. Whence the result 0.0
+ 1
Simba and Ani Jona 🕊 Your answers aren't clearing out my doubt...anywayyy Thanks.
+ 1
Then perhaps you can make yourself more clear? For example, I have specifically asked you for the task description. We can all only take guesses at the moment. And there are two possibilities:
- the expected output of 0.96 is not what you are calculating
- you use integer division but expect a double type result.
But perhaps the problem is an entirely different one. But without knowledge of what you are trying to achieve, how are we supposed to help?
+ 1
Ani Jona 🕊 https://code.sololearn.com/c8VI7rFEkcn6/?ref=app
perhaps you can understand now....
check line 21 and 22.
+ 1
Yes, that helps :)
See, you take in both tax_percent and tip_percent as integer. 100 is an integer too. As I mentioned in my first answer, if all types involved in a calculation are integers, it will be performed as integer calculation, regardless of the return type.
tax_percent is smaller than 100, hence, as integer division, tax_percent/100 is always 0. That causes the result to be 0.0. It suffices to have at least one floating point type involved in the division, for example
double tax=tax_percent/100.0*tip_percent;
Note, that I used 100.0 which is a double type value. You can also accept tax and tip as doubles in the method signature.
But your calculation is not correct, either.
// add tip
meal_cost += meal_cost * tip_percent / 100;
// add tax
meal_cost += meal_cost * tax_percent / 100;
// round to nearest
meal_cost = Math.round(meal_cost);
Here, all calculations are based off of meal_cost, which is a double type. Thus, floating point division will be used.
0
You should make tax_ percentage and other one is also double you will get the answer