37

When I make a division in C#, it automaticaly rounds down. See this example:

double i;
i = 200 / 3;
Messagebox.Show(i.ToString());

This shows me a messagebox containing "66". 200 / 3 is actually 66.66666~ however.

Is there a way I can avoid this rounding down and keep a number like 66.6666667?

ONOZ
  • 1,390
  • 2
  • 11
  • 19
  • 14
    The important lesson here is not just that integer arithmetic is done in integers. Rather, it is that the types of the *operands* are more relevant to the program analysis than the type to which the result is being assigned. In C# we almost always reason from "inside" to "outside"; we don't say "oh, I see you're assigning this to a double, so I will do floating point arithmetic". Instead we say "I see you're dividing two integers; you must want the result as an integer. Oh, you want that integer as a double? then we'll convert it to a double." – Eric Lippert Jun 10 '11 at 21:07
  • I'm looking at this "problem" currently too. I can see from the below that the reasons as to why dividing two integers results in an integer, and I'm happy with that. However, what I can't figure out is the following: Why does C#/VB.Net round 200 / 3 into 66? We know, that if we do the following "200 / 3.0" that we end up with 66.6666667. So why is this being rounded down to 66, and not rounding up to 67? If I do the following, I end up with 67: Convert.ToInt32(200 / 3.0) So my decimal to int becomes 67, yet the runtime will convert my number to 66? – Oxonhammer Feb 02 '17 at 16:31
  • [Very related](https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float) (although that one ask for the reason and not for how to fix it) – user202729 Jun 25 '18 at 14:49

10 Answers10

61

i = 200 / 3 is performing integer division.

Try either:

i = (double)200 / 3

or

i = 200.0 / 3

or

i = 200d / 3

Declaring one of the constants as a double will cause the double division operator to be used.

rsbarro
  • 27,021
  • 9
  • 71
  • 75
8

200/3 is integer division, resulting in an integer.

try 200.0/3.0

John Boker
  • 82,559
  • 17
  • 97
  • 130
6

200 / 3 this is an integer division. Change to: 200.0 / 3 to make it a floating point division.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
6

You can specify format string with the desired number of decimal ponits:

double i;
i = 200 / 3.0;
Messagebox.Show(i.ToString("F6"));
Petar Ivanov
  • 91,536
  • 11
  • 82
  • 95
4

Though the answer is actually 66.666, what is happening is that 200 / 3 is being calculated resulting in an integer. The integer is then being placed in the float. The math itself is happening as integer math. To make it a float, use 200.0 / 3. The .0 will cause it to treat 200 as a float, resulting in floating point math.

JoshuaRogers
  • 405
  • 2
  • 5
4

Aside from the double vs int happening in that action, you're thinking of double as a precise unit. Try using the decimal datatype when you really care about accuracy.

More information at this answer: decimal vs double! - Which one should I use and when?

Community
  • 1
  • 1
sclarson
  • 4,362
  • 3
  • 32
  • 44
2

double i = 200.0 / 3;

double i = ((double)200)/3;

What happens is the two integers perform an integer divide, and then the integer answer is assigned to the float. To avoid that, always cast one of the numbers as a double.

Chad
  • 364
  • 2
  • 7
1

Try this

i = 200d/3d;

and it will not round.

Bala R
  • 107,317
  • 23
  • 199
  • 210
0

200 and 3 are both integers, so the result will be an integer. Convert one of them to a decimal.

Mike M.
  • 12,343
  • 1
  • 24
  • 28
-2

All given answers are wrong because they translate the integer division into one of kind double, which is cleanly not what was asked for (at least from a performance standpoint). The obvious answer is elementary school math, multiply by 10, add 5 and divide again, all integer.

i = (2000 / 3 + 5 ) / 10

You are catching a second division here, which is better than doing double conversions but still far from perfect. You could go even further and multiply by another factor and add other values than five, thus allowing you to use right shifting instead of dividing by 10. The exact formula for doing this is left as an exercise to the reader. (Just google "divisions with Multiply Shift")

Have a nice day.

Tekahe
  • 1
  • 1
    No. It's clear from the OP's sample code that he wants a `double` result without the truncation to an `int`. – Mike Cowan Oct 28 '14 at 04:45