#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.

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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

Follow

Get every new post delivered to your Inbox.

Join 43 other followers