How to represent double in string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I declared 2 integers 'numerator' and 'denominator' and a double 'result'.
If Numerator = 5 and deniminator = 145 then why when I put result in a
string do I get 0?
Example for a label text property:
myLabel.Text= numerator + " / " + denominator + " = " + result;
I get this: 5 / 145 = 0

Where's the decimal? This is a no brainer but I guess I'm lacking in that
department.

Thanx,
Poe
 
myLabel.Text= numerator + " / " + denominator + " = " + result;
Try:
myLabel.Text = Convert.ToString(numerator) + " / " +
Convert.ToString(denominator) + " = " + Convert.ToString(result);
 
Hi Poewood,

Since both numerator and denominator are type int the expression 5/14
results in type int. The value is less than 1 and gets truncated, leaving
0. If you want the expression to evaluate to a double, do this:

double result = (double)numerator / (double)denominator;

Joe
 
Back
Top