C# 将 int 变量转换为 double

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12339868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:56:19  来源:igfitidea点击:

Cast int variable to double

c#castingtype-conversion

提问by Nistor Alexandru

I am a beginner C# programmer, and I am trying to create a calculator. I can't seem to figure out how to cast an intvariable to a double. This is what I have so far:

我是一名初学 C# 程序员,我正在尝试创建一个计算器。我似乎无法弄清楚如何将int变量转换为double. 这是我到目前为止:

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 resultand valueare intvariables.

编辑:resultvalue都是int变量。

采纳答案by tehdoommarine

(double)rezultat /= ...

is not good. The result of a casting expression is always an rvalue, i. e. 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.

不好。转换表达式的结果总是一个右值,即不能赋值的东西。相关:你不能改变表达式的类型(你可以转换它,但这不会真正改变它的类型,只是暂时充当另一种类型)。一旦将变量声明为 an int,您将无法在其中存储双精度值 - 无论您如何转换除法等,它最终都会被截断。

You most likely have to introduce a doubletemporary variable to store the result of the division.

您很可能必须引入一个double临时变量来存储除法的结果。

回答by svick

This depends on the type of the rezultatvariable. If it's double, then you don't have to do anything, integer division won't be used in any case. But if it's int, then your cast doesn't make any sense, you can't store a doublevalue in an intvariable.

这取决于rezultat变量的类型。如果是double,那么您不必做任何事情,在任何情况下都不会使用整数除法。但如果是int,那么你的演员表没有任何意义,你不能doubleint变量中存储一个值。

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 doublevariable for that. And if you have that, your ifwon't make any sense anymore, just use doubledivision in all cases.

因此,正确的解决方案取决于您究竟想做什么。但是,如果您的目标是将实际除法的结果作为double,那么您将需要一些double变量。如果你有那个,你if将不再有任何意义,只需double在所有情况下使用除法。

回答by svick

Try this:

尝试这个:

double rezultat = 1992;
rezultat /= value;

resultatmust be a doubleto store the result of rezultat / value. Otherwise, if both resultatand valueare int, you won't get floating point numbers. For example, 5 / 3 = 1, but (double)5 / 3 = 1.666667. Notice that the value 1.6666667is just a double.

resultat必须是 adouble来存储 的结果rezultat / value。否则,如果两个resultatvalueint,你不会得到浮点数。例如,5 / 3 = 1,但是(double)5 / 3 = 1.666667。请注意,该值1.6666667只是一个double

回答by MikeD

If both of your variables are not doubles, assign them into a doublevariable and then divide.

如果您的两个变量都不是doubles,请将它们分配给一个double变量,然后进行除法。

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

(double)rezultat /= value

I presume you are trying to make rezultata doubleand I presume it's not declared as one and you just can't do that. Your resulting variable that will hold the resultmust also be a doubleor you will just get a whole number not rounded.

我想你正在尝试制作rezultat一个double,我想它没有被声明为一个,你就是做不到。您将保存的结果变量也result必须是 a ,double否则您只会得到一个未四舍五入的整数。