Insert int variable into double

Hi I am a beginner C # programmer and I am trying to create a calculator. I cannot figure out how to convert an int variable to 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 error } } 

How can I do this job?

EDIT: both results and values ​​are int variables

+4
source share
5 answers
 (double)rezultat /= ... 

not very good. The result of a cast expression is always rvalue, i. e. that which cannot be assigned. Related: you cannot change the type of expression (you can use it, but it will not change its type, just temporarily act as another type). After you have declared your variable as, say, int , you cannot save a double in it - however, you divided the division, etc., In the end, it will always be truncated.

You will most likely have to enter a temporary double variable to store the division result.

+9
source

It depends on the type of the rezultat variable. If it is double , then you do not need to do anything, integer division will not be used in any case. But if it is int , then your tide does not make any sense; you cannot store the double value in an int variable.

So, the right decision depends on what exactly you want to do. But if your goal is to get the result of the actual division as double , for this you need the double variable. And if you have this, your if will no longer make any sense, just use double division in all cases.

0
source

Try the following:

  double rezultat = 1992; rezultat /= value; 

Since resultat must be double in order to save the result rezultat / value . Otherwise, if both resultat and value are ints, you will not get floating point numbers. For example, 5/3 = 1, but (double) 5/3 = 1,666667. Please note that the value 1.6666667 is just double.

0
source

If both of your variables are not double s, assign them to the double variable, and then split.

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

I assume that you are trying to do rezultat a double , and I assume that it is not declared as one, and you simply cannot do it. Your resulting variable, which will contain the result , should also be double or you just get an integer that is not rounded.

0
source

Source: https://habr.com/ru/post/1433229/


All Articles