Reputation: 145
I'm new to java so please bear with me. I'm trying to get the percentage of wins from total number of games and something I'm doing is way off. My method for getting the percentage is below:
public double winPercentage(int wins, int total)
{
return (wins % total) * 1.00;
}
If I win 52 games out of 254 my answer comes up to 52.0, using my calculator that same answer is 20.47 assuming wins/total*100. If I switch the modulus to / I constantly get 0.0
I've tried a variation of different decimal places and order of operations. I can't seem to get the calculator and method to match up.
Upvotes: 1
Views: 191
Reputation: 281
public double winPercentage(double wins, double total) {
return wins / total * 100;
}
Upvotes: 1
Reputation: 500377
The percent sign in wins % total
has no relationship to computing a percentage.
To compute percentage in Java, you could write
return 100.0 * wins / total;
Here, 100.0
serves dual purpose:
wins < total
).Upvotes: 5