Visual Basic .NET/Arithmetic operators

Arithmetic operators

edit

Visual Basic .NET provides a basic set of operators to calculate simple arithmetic.

+   Addition
-   Subtraction
*   Multiplication
/   Division
\   Integer division
Mod Remainder Division
^   Exponentiation
&   String concatenation

7 + 2     produces 9
7 - 2     produces 5
7 * 2     produces 14
7 / 2     produces 3.5
7 \ 2     produces 3
7 Mod 2   produces 1
7 ^ 2     produces 49
"7" & "7" produces "77"

Let's look at a short example of arithmetic operations before we jump into the operators themselves.

In this example we will also be using some basic variables. The Dim operator creates the variable.

  Dim Commission As Single
  Dim Sales As Single
  Sales = 3142.51
  Commission = 0.3 * Sales  ' Calculate 30% commission.

First, we set the total sales to 3142.51.

The * operator calculates multiplication, so the last line is equivalent to  . This means that our second step is multiplying 0.3 and Sales. Sales is 3142.51, so our result should be the product of 0.3 and 3142.51.

Why the funny symbols?

edit

With the exception of addition and subtraction, the symbols used are different to the ones used in real life. This is simply because the other symbols are not available on a standard keyboard.

A good reference for the operators in Visual Basic .NET is Windows's built-in Calculator. If you set Calculator on scientific mode, you have access to the same operators as in .NET.


Addition

edit

This adds two numbers together, and is denoted by the "+" symbol. If strings are involved it may also do String concatenation. Examples:

  x = 7 + 2     ' Results in 9.
  x = 25 + -4   ' Results in 21.
  Dim StringA As String
  StringA = "A string" + "Another string" ' Results in "A stringAnother string"

By using funny symbol it will become easy to parse during execution. mostly that is the reason why they have used.

There is a second addition operator, "+=". It increments the variable on the left of the += by the amount indicated on the right.

  Dim x As Integer = 54
  x += 89       ' result is 143
  x += 7       ' result is 150

It also works with Strings as a concatenation operator.

  Dim x As String = "A fox"
  x += " jumped"          ' result is "A fox jumped"
  x += " over the fence"  ' result is "A fox jumped over the fence"

Subtraction

edit

This subtracts two numbers, and is denoted by the "-" symbol. Examples:

  Dim x As Double = 12.045
Dim Y As Integer= 12
  x = x-Y     ' Results in .045.
  x = 25 - -4  ' Results in 29.
    Sub Main()
        Dim x As Double = 12.045
        Dim y As Integer = 12

        System.Console.WriteLine(x - y)
        System.Console.ReadKey()

    End Sub

Beware when subtracting an integer from a double. The above code, when printed, will result in an answer of .04499999999999999 instead of the obvious answer of .045. Using doubles to store decimal values can be highly inaccurate.

Multiplication

edit

This multiplies two numbers, and is denoted by the "*" symbol. Examples:

  Dim x As Integer
  x = 7 * 2   ' Results in 14.
  x = 25 * -4 ' Results in -100.

Division

edit

There are more types of division than the one denoted by the "/" symbol. There is also integer division and remainder division.

Division
This is the most commonly used form of division and is denoted by the "/" operator. Examples:
  Dim x As Single
  ' (note that we must use the Single class to have decimals)
  x = 7 / 2  ' Results in 3.5.
  x = 25 / 4 ' Results in 6.25.
Integer division
This divides two numbers, and gives the result without the remainder if the quotient is a decimal. Examples:
  Dim x As Integer
  x = 7 \ 2    ' Results in 3.
  x = 25 \ 4   ' Results in 6.
Remainder Division
This divides two numbers, and gives the result's remainder if the quotient is a decimal. This is denoted by the operator "Mod." Examples:
  Dim x As Integer
  x = 7 Mod 2  ' Results in 1.
  x = 25 Mod 4 ' Results in 1.

Exponentiation

edit

This is raising a number to a power. For example   in VB .Net code is:

  Dim x As Integer
  x = 7 ^ 2   ' Results in 49.

This results in the number 49 being assigned to the variable x. It can also be used to calculate the square root of a number. The square root of a number is the number raised to the power of 0.5.

  Dim x As Single
  x = 7 ^ 0.5 ' Results in a number around 2.645.

Note: It is necessary to ensure that the variables be correctly declared to get the desired results. The following example works, but will produce the wrong result. This is because the Integer class does not allow decimal places (just like mathematical integers).

  Dim x As Integer
  x = 7 ^ 0.5 ' Results in 3.

Since x is declared as an Integer type, the value square root, a real number, is stored incorrectly.

Any nth root of number is the can be calculated by raising the number to the power of  :

  Dim x As Single
  Dim n As Single
  n = 7
  x = 2 ^ (1 / n)

This is because  .

Rounding

edit
  • Round(): round to the nearest integer.
  • Floor(): round to inferior.
  • Ceiling(): round to superior.
  • Truncate(): truncate the decimal digits.
Module Module1
    Sub Main()
        Dim Number1 As Single = 1.5
        Console.WriteLine(Math.Round(Number1))  ' 2
        Console.WriteLine(Math.Floor(Number1))  ' 1
        Console.WriteLine(Math.Ceiling(Number1))  ' 2
        Console.WriteLine(Math.Truncate(Number1))  ' 1
        Console.ReadLine()
    End Sub
End Module

Comparisons

edit
  • Max()
  • Min()
Module Module1
    Sub Main()
        Console.WriteLine(Math.Max(3, 4))
        Console.ReadLine()  ' 4
    End Sub
End Module