问题
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 value
variable 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