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
2009-03-19 04:12:02
+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:
NoahD
2009-03-19 04:14:16
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
2009-03-19 04:34:09
@Pax, If any of the args in C or C# are a double, a double divide is used (resulting in a double).
strager
2009-03-19 05:18:57