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!

Recommended Answers

All 2 Replies

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 .

THANKSSSSS, you're soooo brilliant!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.