0

hi there. Just wanna check if I am coding this wrongly,

int sumOfVotes = 23
      int howlong = 94
double SimPercent = ((sumOfVotes / howlong) *100);

Shouldn't the answer, SimPercent, be 24.4?

Tks!

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by jemimaloh
0

The problem is you are doing integer division, which will truncate rather than round as you expect. In Java, 5/2 = 2 and 2/5 = 0 . To fix this problem, you need to make one of the operands in the division a floating point type (either float or double). Again, in Java, (float)5/2 = 2.5 and 2/(double)5 = 0.5 .

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.