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 int variable to a 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.
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.You most likely have to introduce a
doubletemporary variable to store the result of the division.