#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

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #55 – Integer Division Results in Truncation

  1. kai zhou says:

    Nice work. Thank you Sean.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: