Progress Bar C# Variable

蹲街弑〆低调 提交于 2019-12-13 22:38:20

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/15878864/progress-bar-c-sharp-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!