且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

进度条 C# 变量

更新时间:2023-12-02 22:39:28

Valueint 变量,因此 TestP1.corAns/52 将即使 TestP1.corAns 是实数(floatdouble),也会四舍五入为某个整数值.此外,如果 TestP1.corAns 也是整数,您将进行整数除法.最终,value 变量的值将四舍五入为最大整数,小于您的操作结果,由于您需要百分比,因此大概为 0.为了避免这种情况,首先确保得到除法后的实数,然后将该数乘以 100.使用类似这样的方法:

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