6
int x = 73;  
int y = 100;  
double pct = x/y;  

Why do I see 0 instead of .73?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • 2
    Not only C#, but a batch of other languages including C and C++. Common Lisp will return 73/100, and languages like Perl that lack an actual integer type will return 0.73. – David Thornley Nov 23 '09 at 20:59
  • 15
    How does one gain over 6K rep and not already know the answer to this question? – AndyG Nov 23 '09 at 21:05
  • @SouceMaster: Because rep (a) doesn't measure ability, it measures participation, and (b) because OO simply asks bazillions of questions (10x as many as answers provided). There is a discussion on this on meta.stackoverflow.com. – Lawrence Dol Nov 23 '09 at 21:07
  • 1
    a junior member of my team asked this after googling for a little while. I suggested that it be posted on SOF for a quicker and more accurate solution. i was correct. Now when he googles this, this post comes up first (and thus SOF is making googling better) – leora Nov 23 '09 at 21:33
  • 1
    Maybe the junior members of your team should have SOF accounts. – mtrw Nov 24 '09 at 03:18
  • @oo: couldn't have just told him the answer? :p – mpen Nov 24 '09 at 03:21
  • but if i just told him, the folks that google this in the future dont get this page as a solution at the top of their list . . . saving time for future programmers everywhere . . – leora Nov 24 '09 at 03:51
  • Duplicate: http://stackoverflow.com/questions/661028/how-can-i-divide-two-integers-to-get-a-double – mtrw Nov 24 '09 at 04:14
  • @oo: Is that story true, or are you just rep-whoring? –  Nov 29 '09 at 05:57
  • @oo: your story is too fake to believe. "Qucker & More Accurate"?? wth!! its just a simple and a single answer. Tell me how could you make answer to this "More Accurate" or "Less Accurate". – claws Nov 29 '09 at 06:26
  • faster then . . see http://stackoverflow.com/questions/741083/is-it-bad-to-ask-google-searchable-questions-on-stack-overflow-closed – leora Nov 29 '09 at 13:09

6 Answers6

34

Because the division is done with integers then converted to a double. Try this instead:

double pct = (double)x / (double)y;
Brian Ensink
  • 11,092
  • 3
  • 50
  • 63
  • 7
    Just for completeness: Converting one of the ints to double is enough, but it doesn't hurt to convert them both :) – schnaader Nov 23 '09 at 20:53
8

It does the same in all C-like languages. If you divide two integers, the result is an integer. 0.73 is not an integer.

The common work-around is to multiply one of the two numbers by 1.0 to make it a floating point type, or just cast it.

nobody
  • 19,814
  • 17
  • 56
  • 77
Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
3

because the operation is still on int type. Try double pct = (double)x / (double)y;

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
DJ.
  • 6,664
  • 1
  • 33
  • 48
2

Integer division drops the fractional portion of the result. See: http://mathworld.wolfram.com/IntegerDivision.html

mmorrisson
  • 541
  • 8
  • 19
2

It's important to understand the flow of execution in a line of code. You're correct to assume that setting the right side of the equation equal to double (on the left side) will implicitly convert the solution as a double. However, the flow execution dicates that x/y is evaluated by itself before you even get to the double pct = portion of the code. Thus, since two ints are divided by each other, they will evaluate to an int solution (in this case, rounding towards zero) before being implicitly converted to a double.

As other have noted, you'll need to cast the int variables as doubles so the solution comes out as a double and not as an int.

Ben McCormack
  • 32,086
  • 48
  • 148
  • 223
1

That’s because the type of the left hand operand of the division (x) is of type int, so the return type of x / y is still int. The fact that the destination variable is of type double doesn’t affect the operation. To get the intended result, you first have to cast (convert) x to double, as in:

double pct = (double)x / y;
  • Which operand is double doesn't matter. `Either (double)73/100` or `73/(double)100` would work just fine. If one operand is a double, all integer types will be coerced to double. – David Thornley Nov 23 '09 at 21:00