烦人的双重价值
好的,谁能解释一下为什么变量偏移量返回为 0? 我需要更新进度条,但该值小于 100,因此 offset 是增加当前值的值,然后用当前的下限值更新进度条,但当它返回 0 时,它不会更新!
double offset = 0.000001;
int hmm = (image.Height * image.Width);
double current = 0;
MessageBox.Show(offset.ToString());
MessageBox.Show(hmm.ToString());
offset = 100 / hmm;// 0.01;// 100 / (image.Height * image.Width) * 10000;
MessageBox.Show(offset.ToString());
Ok can anyone explain why the variable offset comes back as 0?
I need to update a progress bar but the value is less than 100 so offset is the value to increase current by and then update the progress bar with the floored value of current but as it comes back 0 it's not updating!
double offset = 0.000001;
int hmm = (image.Height * image.Width);
double current = 0;
MessageBox.Show(offset.ToString());
MessageBox.Show(hmm.ToString());
offset = 100 / hmm;// 0.01;// 100 / (image.Height * image.Width) * 10000;
MessageBox.Show(offset.ToString());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您正在执行整数除法 -
hmm
和100
都是整数。因此,如果hmm
大于 100,结果将始终为 0。将任一操作数转换为double
,它将使用浮点运算。例如:You're performing integer division - both
hmm
and100
are integers. Therefore ifhmm
is greater than 100, it will always give 0 as the result. Convert either operand to adouble
and it'll use floating point arithmetic. For example:尝试使用
问题是你正在使用整数除法。
try using
The problem is you're using integer division.
您正在执行
100
和hmm
之间的整数除法。结果始终是一个整数,并且您会看到它产生 0,因为在您的情况下hmm
大于100
。试试这个:
You are performing an integer division between
100
andhmm
. The result would always be an integer, and you are seeing it produce 0 becausehmm
is greater than100
in your case.Try this instead:
问题出在最后一行代码。如果您写 100 / hmm,结果将被视为整数值,因为 100 是整数。尝试使用
The problem is the last line of code. If you write 100 / hmm the result will be seen as integer value as 100 is an integer. Try using
整数除法总是去掉小数点。因此,像
1 / 100
= .01 这样的东西就会变成 0。Integer division always drops the decimal point. Therefore, something like
1 / 100
= .01 would just become 0.嗯是一个整数。尝试将其声明为 float 或 double,或者在执行计算时将其转换为 float 或 double。
IE。
hmm is an int. Try declaring it as a float or double, or cast it as such when you perform the calculation.
IE.