Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to get the value for a progessbar(in C#) from a variable I currently have, divided by 52, and multiplied by 100. This is the code I have, any suggestions to fix it ?

int value;             
value = TestP1.corAns / 52 * 100;             
ProgressBar pBar = new ProgressBar();            
pBar.Value = value;             
label2.Text = Convert.ToString(value) + "%";
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
259 views
Welcome To Ask or Share your Answers For Others

1 Answer

Value is int variable and therefore TestP1.corAns / 52 will be rounded to some integer value even if TestP1.corAns is a real number (float or double). Moreover, if TestP1.corAns is also integer you will have integer division. Ultimately the value of the valuevariable will be rounded to the biggest integer, smaller than the result of your operations, presumably to 0 since you want percents. In order to avoid that, first make sure to get real number after division and that multiply that number by 100. Use something like this:

double value;             
value = TestP1.corAns / 52.0 * 100.0;             
ProgressBar pBar = new ProgressBar();            
pBar.Value = (int)value;             
label2.Text = Convert.ToString(value) + "%";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...