#55 – Integer Division Results in Truncation

When dividing one integer by another using the division operator, C# always rounds the result towards 0 — i.e. truncates.

 int n1 = 7 / 2;       // 3
 long n2 = -7 / 2;     // -3
 short n3 = -11 / -3;  // 3

If an attempt is made to divide by zero, an exception of type System.DivideByZeroException is thrown.

If you try to divide by the literal 0, you’ll get an error at compile time.

Advertisement