I'm willing to be that count and totalwords are both int too, which means your division is performed in integer context. If the division results in a value less than zero, the precision is ignored and the result will be 0. You want something more like this:
int wordpercent = (int)((count / (double)totalwords) * 100)
This forces the division into floating point context, which retains precision so that it isn't lost before the multiplication step.
deceptikon
Challenge Accepted
3,457 posts since Jan 2012
Reputation Points: 822
Solved Threads: 474
Skill Endorsements: 57
It's integer arithmetic, performed in integers throughout.
eg (10/20)x100
10/20 in integer = 0 - there's no fractions in integer values
0x100 = 0
on the other hand
(10x100)/20
10x100 = 1000
1000/20 = 50
JamesCherrill
... trying to help
8,528 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Using a double is a valid solution, but it does have side effects. Personally I would just rearrange the expression so it does the multiply before the divide and keep all the variables as ints.
int wordpercent = (count*100)/totalwords;
JamesCherrill
... trying to help
8,528 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30
Question Answered as of 8 Months Ago by
JamesCherrill
and
deceptikon