Performing mathematical functions in VB.NET

Irina Medvinskaya shows you a simple way of working with math-related functions in VB.NET.

Visual Studio .NET provides an easy way of performing mathematical functions, such as addition, subtraction, multiplication, division, exponentiation, integer division, and finding a remainder. For all other tasks, you can utilize the System.Math class. In this tip, I will look at a simple way of working with math-related functions in VB.NET.

Basic mathematical operations

.NET offers common operators to facilitate the basic mathematical functions, such as:

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Exponentiation (^)
  • Integer Division (\)
  • Finding the remainder (Mod)

However, for other operations, you can use the methods available in the System.Math class.

System.Math class members

Some of the members of the System.Math class include the following:

  • Trigonometric functions (Sin, Cos, Tan, etc)
  • Logarithmic functions (Log and Log10)
  • Constants (PI and E)
  • Power functions (Exp, Pow, and Sqrt)
  • Boundary functions (Floor, Ceiling)
  • Comparative functions (Max, Min)
  • Sign-related functions (Abs)


    Private Sub PerformMathFunctions()

        Dim i As Integer

        i = Math.Pow(2, 3)

        i = Math.Sqrt(16)

        i = Math.Round(5.34444)

    End Sub

How it works

First, I define an integer type variable i. Then, I assign the value of 2 taken to the third power and display the result in the message box (the value displayed is 8). The value of i is then set to the value of the square root of 16, and its value is displayed in the message box (the value displayed is 4). And finally, I set the value of the variable i to the result of rounding the number 5.34444 and show the result in the message box (the value displayed is 5).

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

Editor's Picks

Free Newsletters, In your Inbox