Mathematical Operators tutorials

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes how to use Mathematical Operators

There are several operators that you can use to perform some simple mathematical operations on numeric values.

For example, you can use the plus symbol (+) to perform addition, the hyphen () to perform subtraction, the asterisk (*) to perform multiplication, and the slash (/) to perform division.

      Total + Shipping AS GrandTotal
      (Total - Discount) + Shipping AS GrandTotal

In addition you can use the modulo (%) operator, which returns the remainder when division is performed on the two values.

For example, 11 % 2 returns 1 because 1 is the remainder when you divide 11 by 2. You usually use the modulo to determine whether a number is odd or even if the second value in the expression is 2. If the result is 1, then the value is odd, and if the result is 0, then the value is even.

      WHEN ColumnName % 2 = 0
      THEN 'Even'
      ELSE 'Odd'
    END AS Status

When you work with operators, you must keep the data types of the values in mind.

If you are performing an operation that involves two different data types, the expression will return values for the data type with the highest precedence if possible.

In addition not every value can be rolled into the other value. For example, an INT can be converted to a BIGINT, but BIGINT cannot be converted to INT because a valid BIGINT values is too big to be converted to INT. Therefore, when an operation is performed on a BIGINT and an INT, the result will be a BIGINT.

However it's not always possible to convert the lower precedence data type to the higher precedence data type. For example, if you try to execute the expression 25 + 'abc' it will fail, because those characters cannot be converted to a numeric value.

Share This

Home | About | Contact | Privacy Policy

Copyright 2017 - All Rights Reserved.