烦人的双重价值

发布于 2024-12-11 08:23:19 字数 393 浏览 0 评论 0原文

好的,谁能解释一下为什么变量偏移量返回为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只想待在家 2024-12-18 08:23:19

您正在执行整数除法 - hmm100 都是整数。因此,如果 hmm 大于 100,结果将始终为 0。将任一操作数转换为double,它将使用浮点运算。例如:

double offset = 100.0 / hmm;

You're performing integer division - both hmm and 100 are integers. Therefore if hmm is greater than 100, it will always give 0 as the result. Convert either operand to a double and it'll use floating point arithmetic. For example:

double offset = 100.0 / hmm;
一世旳自豪 2024-12-18 08:23:19

尝试使用

 offset = 100./hmm;

问题是你正在使用整数除法。

try using

 offset = 100./hmm;

The problem is you're using integer division.

江心雾 2024-12-18 08:23:19

您正在执行 100hmm 之间的整数除法。结果始终是一个整数,并且您会看到它产生 0,因为在您的情况下 hmm 大于 100

试试这个:

offset = 100f / hmm; // the trailing f makes 100 a float

You are performing an integer division between 100 and hmm. The result would always be an integer, and you are seeing it produce 0 because hmm is greater than 100 in your case.

Try this instead:

offset = 100f / hmm; // the trailing f makes 100 a float
你怎么这么可爱啊 2024-12-18 08:23:19

问题出在最后一行代码。如果您写 100 / hmm,结果将被视为整数值,因为 100 是整数。尝试使用

((double)100)/hmm;

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

((double)100)/hmm;
○愚か者の日 2024-12-18 08:23:19

整数除法总是去掉小数点。因此,像 1 / 100 = .01 这样的东西就会变成 0。

Integer division always drops the decimal point. Therefore, something like 1 / 100 = .01 would just become 0.

心欲静而疯不止 2024-12-18 08:23:19

嗯是一个整数。尝试将其声明为 float 或 double,或者在执行计算时将其转换为 float 或 double。

IE。

offset = 100 / ((double)hmm);

hmm is an int. Try declaring it as a float or double, or cast it as such when you perform the calculation.

IE.

offset = 100 / ((double)hmm);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文