简体   繁体   English

将 int 变量转换为 double

[英]Cast int variable to double

I am a beginner C# programmer, and I am trying to create a calculator.我是一名初学 C# 程序员,我正在尝试创建一个计算器。 I can't seem to figure out how to cast an int variable to a double .我似乎无法弄清楚如何将int变量转换为double This is what I have so far:这是我到目前为止:

public void oImpartire() {
    if (rezultat % value == 0)
    {
        rezultat /= value;
    }
    else {
       (double)rezultat /= value;  // this should be double but I get an error
    }
}

How can I make this work?我怎样才能使这项工作?

EDIT: Both result and value are int variables.编辑: resultvalue都是int变量。

(double)rezultat /= ...

is not good.不好。 The result of a casting expression is always an rvalue, ie something that cannot be assigned to.转换表达式的结果总是一个右值,即不能赋值的东西。 Related: you can't change the type of an expression (you can cast it, but that won't really change its type, just act as another type temporarily).相关:你不能改变表达式的类型(你可以转换它,但这不会真正改变它的类型,只是暂时充当另一种类型)。 Once you declared your variable as, say, an int , you won't be able to store a double in it - however you cast the division, etc. it will always be truncated in the end.一旦您将变量声明为int ,您将无法在其中存储双精度值 - 无论您如何转换除法等,它最终都会被截断。

You most likely have to introduce a double temporary variable to store the result of the division.您很可能必须引入一个double临时变量来存储除法的结果。

If both of your variables are not double s, assign them into a double variable and then divide.如果您的两个变量都不是double ,请将它们分配给一个double变量,然后进行除法。

VarDouble = (double)int.....; VarDouble /= VarDouble1 etc

(double)rezultat /= value

I presume you are trying to make rezultat a double and I presume it's not declared as one and you just can't do that.我想你正试图让rezultat成为一个double ,我认为它没有被声明为一个,你就是不能那样做。 Your resulting variable that will hold the result must also be a double or you will just get a whole number not rounded.保存result变量也必须是double否则您将得到一个未四舍五入的整数。

This depends on the type of the rezultat variable.这取决于rezultat变量的类型。 If it's double , then you don't have to do anything, integer division won't be used in any case.如果它是double ,那么你不必做任何事情,在任何情况下都不会使用整数除法。 But if it's int , then your cast doesn't make any sense, you can't store a double value in an int variable.但是如果它是int ,那么你的演员表没有任何意义,你不能在int变量中存储double int值。

So, the correct solution depends on what exactly do you want to do.因此,正确的解决方案取决于您究竟想做什么。 But if your goal is to have the result of the actual division as a double , you will need some double variable for that.但是,如果您的目标是将实际除法的结果作为double ,那么您将需要一些double变量。 And if you have that, your if won't make any sense anymore, just use double division in all cases.如果你有那个,你的if将不再有任何意义,只需在所有情况下使用double除法。

Try this:试试这个:

double rezultat = 1992;
rezultat /= value;

resultat must be a double to store the result of rezultat / value . resultat必须是double才能存储rezultat / value的结果。 Otherwise, if both resultat and value are int , you won't get floating point numbers.否则,如果resultatvalue都是int ,您将不会得到浮点数。 For example, 5 / 3 = 1 , but (double)5 / 3 = 1.666667 .例如, 5 / 3 = 1 ,但是(double)5 / 3 = 1.666667 Notice that the value 1.6666667 is just a double .请注意,值1.6666667只是一个double

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

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