How can I get a double by dividing two integers?
Asked by leora
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
Complementing the @NoahD’s answer
You can cast to decimal to get more precision:
Decimal.Divide(100, 863) //0.1158748551564310544611819235
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
The integers are converted to doubles.
Answered by Stephen Wrighton
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
var firstNumber=5000, secondeNumber=37; var decimalResult = decimal.Divide(firstNumber,secondeNumber); Console.WriteLine(decimalResult );
Answered by Rejwanul Reja
Post is based on https://stackoverflow.com/questions/661028/how-can-i-divide-two-integers-to-get-a-double