c# - Convert a string in a label to int not working properly -
i have following code:
int = int32.parse(weight.text); double b = 0; if (this.wcf.selecteditem == weighing) { b = (a * 1.03) / 1000; wll.content = b.tostring(); }
weight
name of textbox
, in textbox
input made 50000
. wll
label
calculation of b
shown. wll
shows 51.5
correct.
i want use value of b
in calculation further down , therefore have defined new int
:
int p = convert.toint32(b); double g = 0; double sq = math.sqrt(50 / p);
the value of sq should 0,985
, shown in label daf
, program shows 1,492
. not correct here, can 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 make have math.sqrt(int)
, losing precision. instead, 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
double
in addition, since b
can have result of non-integer, may want change line too:
int p = convert.toint32(b); //same issue losing precision
into
double p = convert.todouble(b); //use double instead
Comments
Post a Comment