简体   繁体   English

为什么返回C#Convert.ToDouble(5/100)0.0而不是0.05

[英]Why returns C# Convert.ToDouble(5/100) 0.0 and not 0.05

double variable = Convert.ToDouble(5/100);

Will return 0.0 but i expected 0.05 将返回0.0但我预计0.05

What can / must i change to get 0.05 什么可以/必须改变得到0.05

because the 5 in this example is a variable 因为这个例子中的5是一个变量

5/100 is done in integer arithmetic, which yields 0 before conversion. 5/100以整数运算完成,转换产生0。 Try 尝试

double variable = 5.0/100;

If 5 is in a variable x (of integer type), then use: 如果5在变量x (整数类型)中,则使用:

variable = (double)x/100; 

or 要么

variable = ((double)x)/100;

to make the intent clear (thanks John!) 使意图明确(感谢约翰!)

or 要么

variable = x/100.0;

Unlike real-world, computers treat mathematical operations a bit differently though there's no significant difference once we understand why it behaves so. 与现实世界不同,计算机对数学运算的处理方式略有不同,但一旦我们理解了它的行为,就没有显着差异。

1.) Why it behaves like this ? 1.)为什么它表现得像这样?

Note that, integers are whole numbers and the variables of type integer can only store whole numbers and can not store or recognize decimal numbers. 请注意,整数是整数,整数类型的变量只能存储整数 ,不能存储或识别十进制数。 when you say 5/100, both 5 and 100 are integer literals for the computers and it is called integer division . 当你说5/100时,5和100都是计算机的整数文字,它被称为整数除法 The result should be 0.05 but since this is an integer division the result would also be integer and as I said integers cannot store decimal point values, the trailing part after "." 结果应该是0.05,但由于这是一个整数除法,结果也将是整数 ,正如我所说的整数不能存储小数点值, 后面的部分是“。”。 (decimal point) is ignored completely and hence the result is 0 . (小数点)被完全忽略,因此结果为0

Adding more to this, though you're converting the result to double, it does not make any difference because before it is actually converted to a double the result is already 0 and it happens to convert integer 0 to double which ultimately results into 0.0 . 添加更多内容,虽然您将结果转换为double,但它没有任何区别,因为在它实际转换为double之前,结果已经为0并且恰好将整数0转换为double,最终导致0.0

2.) How to get your desired output ? 2.)如何获得所需的输出?

Other answers explain the solution very well, so I kindly request you to refer to those answers rather re-inventing the wheel for you. 其他答案很好地解释了解决方案,所以我请求您参考这些答案,而不是为您重新发明轮子。

Hope this helps. 希望这可以帮助。

因为整数除法中的5/100是0.你需要确保你在双打上进行除法。

5/100 is integer arithmetic. 5/100是整数运算。 In order to have double precision one or more of the values need to be doubles. 为了具有双精度,一个或多个值需要加倍。

double result = 5.0/100.0;  
double result = 5.0/100;  
double result = 5/100.0;  
double result = (double)5/100;  
double result = 5/(double)100;

or 要么

double numerator = 5;  
double denominator = 100;  
double result = numerator / denominator;

双变量= 5D / 100D;

so here would be my revised 'considered' answer... 所以这将是我修改后的“考虑”答案......

as we don't know what 'type' of variable is arriving as the numerator, we'd have to use the Double.TryParse on that. 因为我们不知道变量的“类型”是作为分子到达的,所以我们必须使用Double.TryParse。 In the lab, we could cook something like this up: 在实验室里,我们可以做这样的事情:

var numerator = "5"; // let's make it string to prove the point
double parsedNumerator;
int denominator = 100; // this could well be a constant

double result;
if(Double.TryParse(numerator, out parsedNumerator))
{
    // notice no casting or convert fluff
    result = parsedNumerator/denominator;
    // do something with the result
}
else
{
    // warn that the numerator is doo-lallie
}

now hiding under the desk - just in case i've overlooked something else obvious!! 现在躲在桌子底下 - 以防万一我忽略了其他明显的东西!! :) :)

jim 吉姆

Marco, 马可,

try this instead: 试试这个:

double variable = Convert.ToDouble((double)5/100);

jim 吉姆

[late edit] - as was pointed out (by silent voters :)) the use of the Convert is redunant. [后期编辑] - 正如所指出的(由沉默的选民:) :)转换的使用是多余的。 It should obviously be done in a fashion similar to the other entries. 显然应该以类似于其他条目的方式完成。 I acknowledge that but leave it in above as an example of how to get quickly downvoted when answering a question in a hurry... let caution be your master!! 我承认这一点,但请将其留在上面作为一个例子,说明如何在匆忙回答问题时迅速投票...请谨慎对待您的主人!

Also, as we don't know whether the variable will come from a string or a number, he should also consider tryparse etc on the number 1st before doing the arithmetic. 另外,由于我们不知道变量是来自字符串还是数字,所以在进行算术运算之前,他还应该在第1个数字上考虑tryparse等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM