20

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.

10
  • 8
    Read the error message.
    – Adam
    Sep 9, 2012 at 14:22
  • Why converting int to double any way? Integer does not have any decimals. Anyway, you can use double.Parse() method. Sep 9, 2012 at 14:23
  • @Mitja no you can't; double.Parse works with strings only.
    – Adam
    Sep 9, 2012 at 14:24
  • We do not know the types of rezultat and value because you did not include the code segment in which they are declared. Such information is important when asking a question which involves casting.
    – Rotem
    Sep 9, 2012 at 14:25
  • @H2CO3 I don't find that an obvious assumption considering the question.
    – Rotem
    Sep 9, 2012 at 14:27

5 Answers 5

26
   Convert.ToDouble(int);

http://msdn.microsoft.com/en-us/library/system.convert.todouble.aspx

3
  • yes but will not actualy convert my variable to a double variable it will convert the value isen't that righT? Sep 9, 2012 at 14:23
  • 1
    This doesn't answer the question.
    – user529758
    Sep 9, 2012 at 14:24
  • 3
    @user985482 you can not convert the type of a variable. C# is statically typed.
    – Rotem
    Sep 9, 2012 at 14:24
14
(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.

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

2
  • As a side note, if the OP is developing a calculator application, he is much better off using the decimal type rather than a double.
    – Rotem
    Sep 9, 2012 at 14:33
  • @Rotem yes. I still don't see why he needs to perform modulo on doubles, though..
    – user529758
    Sep 9, 2012 at 14:37
2

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

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. Your resulting variable that will hold the result must also be a double or you will just get a whole number not rounded.

0

This depends on the type of the rezultat variable. 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 double value in an int variable.

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. And if you have that, your if won't make any sense anymore, just use double division in all cases.

0

Try this:

double rezultat = 1992;
rezultat /= value;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.