divide int and double!! can it be done?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asafok
    New Member
    • Aug 2007
    • 10

    divide int and double!! can it be done?

    as far as i know it can't be done ( the division of 2 variables from diffrent types).

    i used this code in c# and it worked:

    int x = 7;
    double y = 6.5;
    Console.WriteLi ne(x / y);


    my question is how can i divide 2 variables from diffrent types without explicit converting them to the same type??
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    That code works, what is your problem?
    If you don't specify an output type, your result could be truncated to the most restricted data type.
    In that example the output is truncated to an int value. You can think of it like significant digits, you can only have as many as the least percise value.

    You might find better results like this:
    Code:
    int x = 4;
    double y = 7;
    Console.WriteLine(    (double)(x / y)  );
    //or
    Console.WriteLine(    ((double)x) / y  );
    You need to explicit cast the value to get a better result.


    ALSO:
    I have removed your double post, in the future please refrain from posting the question more then once.
    MODERATOR

    Comment

    Working...