How can I divide two integers to get a double?

Viewed 302296

How do I divide two integers to get a double?

8 Answers

You want to cast the numbers:

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

Note: If any of the arguments in C# is a double, 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

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

 real_result = (int_numerator + 0.0) / int_denominator
var result = decimal.ToDouble(decimal.Divide(5, 2));

I have went through most of the answers and im pretty sure that it's unachievable. Whatever you try to divide two int into double or float is not gonna happen. But you have tons of methods to make the calculation happen, just cast them into float or double before the calculation will be fine.

The easiest way to do that is adding decimal places to your integer.

Ex.:

var v1 = 1 / 30 //the result is 0
var v2 = 1.00 / 30.00 //the result is 0.033333333333333333
Related