Software Development

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!


hi i am suganya i want calculate mess fees in project but the data's retrive from another on table plzzzzzzzzzz help me


In VB6 I do this: Dim A A = 1000 * 1024 MsgBox A This does not work as it complains about line 2 when it runs! Is it the same in VB.Net and why does it do it?


From the MSDN Visual Studio 6 Help concerning the Error 6 Overflow that you're probably getting: ---------------------- Overflow (Error 6) An overflow results when you try to make an assignment that exceeds the limitations of the target of the assignment. This error has the following causes and solutions: The result of an assignment, calculation, ordata type conversion is too large to be represented within the range of values allowed for that type ofvariable. Assign the value to a variable of a type that can hold a larger range of values. An assignment to aproperty exceeds the maximum value the property can accept. Make sure your assignment fits the range for the property to which it is made. You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer. For example: Dim x As Long x = 2000 * 365 ' Error: Overflow To work around this situation, type the number, like this: Dim x As Long x = CLng(2000) * 365 ----------------------------- When you simply declare A, it's a Variant by default. When you multiply 1000 and 1024, both numbers are integers so A gets coerced into an Integer too. But the product of 1000 and 1024 is too big to fit into an Integer, so you get the overflow. By converting 1000 to a Long, A now gets coerced into a Long which is big enough to handle the product. Also works if you convert 1024 instead of 1000 to a Long. In addition, converting either to a Double (CDbl) or a Single (CSng) works. What seems odd, though, is you'd think that if you dimmed A as Long right at the start, everything would be OK, but you still get the overflow. Evidently the coercion to Integer happens regardless of how you dim A. Haven't tried it in VB.NET.