为什么除法总是零? [英] Why division is always ZERO?
本文介绍了为什么除法总是零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
int value = 10 * (50 / 100);
预期答案为5,但始终为零.谁能给我详细解释原因吗?
The expected answer is 5, but it is always zero. Could anyone please give me detail explanation why it is?
非常感谢.
推荐答案
因为 50/100
的结果为0.
50/100
等于 int(50/100)
,它返回 0
.
此外,如果您想返回 5
,请使用以下代码:
Also, if you want to return 5
, use this:
int value = (int)(10 * (50 / 100.0));
(50/100.0)
的结果为 0.5
.
这篇关于为什么除法总是零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文