Implicit conversion from int expression to double

Guys,

I'm coming from J2SE so C# syntax is a new thing for me. Here is my situation. It is my believe that when you divide an integer with an integer like this below and store it in a double variable, C# compiler or .Net runtime should do implicit conversion for you.

double k = (2/10); // k should be 0.2

However, when I run the program, k turns out to be 0.0 instead of 0.2. I have to code it like this in order for it to work

double k = (double) king / (double) general; // "king" and "general" are int variables.

My question is... is there any way to configure the compiler or put anything in my code so that I don't have to cast it everytime I do something like this

Thank you,



Answer this question

Implicit conversion from int expression to double

  • trager

    *grin* well, that's where my MVP is, so that'd be my suggestion...

    Seriously tho, you could write a helper function if you did this a lot. It's more a weakness in the libraries than the language itself.



  • Kamal Hathi

    Thank you very much, but a little bit dissappointed by the power of the language. May be I should move to C++ instead LOL
  • ThomasBear

    No, you can't. You only need to make the value you divide by be a double in order to get a double result, though.



  • Implicit conversion from int expression to double