I have the following code:
int a = Int32.Parse(weight.Text);
double b = 0;
if (this.wcf.SelectedItem == weighing)
{
b = (a * 1.03) / 1000;
wll.Content = b.ToString();
}
weight
is the name of a TextBox
and in this TextBox
where the input is made which is 50000
. wll
is a Label
where the calculation of b
is shown. wll
shows 51.5
which is correct.
I want to use the value of b
in a calculation further down and therefore I have defined a new int
:
int p = Convert.ToInt32(b);
double g = 0;
double sq = Math.Sqrt(50 / p);
The value of sq should then be 0,985
and is shown in the label daf
, but the program shows 1,492
. Something is not correct here, can anyone help me?
g = (1.09 + (0.41 * sq));
daf.Content = g.ToString();
Beware of this:
int p = Convert.ToInt32(b);
double g = 0;
double sq = Math.Sqrt(50 / p); //int/int
this will make you have Math.Sqrt(int)
, losing precision. Instead, you should do:
double sq = Math.Sqrt(50D / p); //double/int
or
double sq = Math.Sqrt((double)50 / p); //double/int
or
double sq = Math.Sqrt(50.0 / p); //double/int
Declare 50
as double
In addition, since your b
can have result of non-integer, you may want to change this line too:
int p = Convert.ToInt32(b); //same issue losing precision
into
double p = Convert.ToDouble(b); //use double instead