tags:

views:

1645

answers:

3

How do I divide two integers to get a double?

+4  A: 

Convert one of them to a double first. This form works in many languages:

 real_result = (int_numerator + 0.0) / int_denominator
Mark Ransom
+5  A: 

cast the integers to doubles.

Stephen Wrighton
+20  A: 

You want to cast the numbers:

    double num3 = (double)num1/(double)num2;

Note: If any of the arguments in C# are doubles, a double divide is used which results in a double. So, the following would work too:

    double num3 = (double)num1/num2;

For more information see:

Dot Net Perls

NoahD
Don't know if this is the same in C#, but C only requires you to cast the first - it'll automatically make double/int a double.
paxdiablo
@Pax, If any of the args in C or C# are a double, a double divide is used (resulting in a double).
strager