Coder Perfect

How can I get a double by dividing two integers?


How can I get a double by dividing two integers?

Asked by leora

Solution #1

You’d want to roll the dice:

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

Note that in C#, if any of the parameters are doubles, a double division is performed, resulting in a double. As a result, the following would also work:

double num3 = (double)num1/num2;

More information can be found at:

Dot Net Perls

Answered by NoahD

Solution #2

Complementing the @NoahD’s answer

You can cast to decimal to get more precision:



Decimal.Divide(100, 863)

Doubles are represented with 64 bits, while decimal is represented with 128 bits.


Take a look at this essay by Jon Skeet on floats and doubles, as well as this one about decimals, for further information on the binary floating point representation and its precision.

Answered by fabriciorissetto

Solution #3

The integers are converted to doubles.

Answered by Stephen Wrighton

Solution #4

Convert one of them to a double first. This form can be used in a variety of languages:

 real_result = (int_numerator + 0.0) / int_denominator

Answered by Mark Ransom

Solution #5

var firstNumber=5000,

var decimalResult = decimal.Divide(firstNumber,secondeNumber);

Console.WriteLine(decimalResult );

Answered by Rejwanul Reja

Post is based on