Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

Progress Bar C# Variable

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

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

1 Reply

0 votes
by (71.8m points)

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) + "%";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...