为什么返回C#Convert.ToDouble(5/100),0.0和0.05不 [英] Why returns C# Convert.ToDouble(5/100) 0.0 and not 0.05
本文介绍了为什么返回C#Convert.ToDouble(5/100),0.0和0.05不的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
double variable = Convert.ToDouble(5/100);
返回0.0,但我预计0.05
Will return 0.0 but i expected 0.05
可以/我必须改变什么来获得0.05
What can / must i change to get 0.05
,因为在这个例子中,5是可变
because the 5 in this example is a variable
推荐答案
5/100
在整数运算完成,这将产生0的前的转换。尝试
5/100
is done in integer arithmetic, which yields 0 before conversion. Try
double variable = 5.0/100;
如果 5
是一个变量 X
(整型),然后使用:
If 5
is in a variable x
(of integer type), then use:
variable = (double)x/100;
的或的
variable = ((double)x)/100;
做的意图明确(感谢约翰!)
to make the intent clear (thanks John!)
的或的
variable = x/100.0;
这篇关于为什么返回C#Convert.ToDouble(5/100),0.0和0.05不的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文