Weird calculation problem

  • Thread starter Thread starter Gas
  • Start date Start date
G

Gas

I am new to C#, I want to perfoem a calculation and I have a weird out come
from the app.

Here is the code

double percentage = 0;

percentage = ctrl1.Height / (ctrl1.Height+ ctrl2.Height)

when I try to output the percentage, it is always 0.
(ctrl1.Height and ctrl2.Height are positive value)

anyone can give me some help here?


Gas
 
Gas

all computations are being done using integer math - and the results will be
either 0 or 1, then stored in the double.

consider casting one variable to a double - and that will get the entire
expression to be evaluated using floating point math instead of integer
math. try something like this:

double percentage = 0;
percentage = (double)ctrl1.Height / (ctrl1.Height+ ctrl2.Height)


regards
roy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top