进度条 C# 变量 [英] Progress Bar C# Variable
问题描述
我正在尝试从我当前拥有的变量中获取 progessbar(在 C# 中)的值,除以 52,再乘以 100.这是我拥有的代码,有什么修复它的建议吗?
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) + "%";
推荐答案
Value
是 int
变量,因此 TestP1.corAns/52
将即使 TestP1.corAns
是实数(float
或 double
),也会四舍五入为某个整数值.此外,如果 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 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) + "%";
这篇关于进度条 C# 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!