0

I have tried this with decimal, double and float variables. I am dividing an integer by another integer and storing the result in a variable. None of the three data types will store decimal points, for example:

double d;
uint num1 = 20
uint num2 = 3
d = num1 / num2;
//d = 6.0

It is as if it is rounding to the nearest integer, help please?

ohSkittle
  • 149
  • 3
  • 14
  • also read up on the following for good reference [C# MSDN Decimal.Divide Method()](http://msdn.microsoft.com/en-us/library/system.decimal.divide%28v=vs.110%29.aspx) – MethodMan Jan 15 '15 at 21:59
  • That did the trick, Thanks! Also, do you know why I have to convert it to a decimal when assigning it if there is no error about needing to explicitly convert it? – ohSkittle Jan 15 '15 at 22:00
  • 2
    There should be at least one person asking the same question in the past... Diviindg 2 integers is somewhat common - so surprising you have not found any previous questions about it. – Alexei Levenkov Jan 15 '15 at 22:01

2 Answers2

2

You should cast either num1 or num2 as a decimal/double/float first before doing the division and storing the result..

JustAPup
  • 1,720
  • 12
  • 19
2

When you do math with integers, the result is an integer. That's just how the operators are defined. To do double math, make num1, num2, or both doubles, or cast one of them to a double before calculating.

adv12
  • 8,443
  • 2
  • 24
  • 48