Answered by:
Math problem
Question

xpercent = ((xincrease / xprogressBar.Maximum) * 100);
In the code above xincrease is an int and equals 349, xprogressBar.Maximum equals 350. I would expect xpercent to equal 99.71, but it equals 0. I have tried declaring xpercent as an int, decimal, double and float, but each time xpercent equals 0.
Can anyone tell me why?
Thanks in advance.
DrewT1755
Answers
All replies


You need to think about what you're doing. Think about doing 349/350 as an integer division. The result is 0.
When you're doing integer computer arithmetic where truncation and roundoff errors are a problem, always do the multiplication first:
xpercent = xincrease * 100 / xprogressBar.Maximum;
Now you're doing 34900/350, which is 99.
Tim Roberts  Driver MVP Emeritus  Providenza & Boekelheide, Inc.