In Visual Basic 2015, we can write codes that can perform arithmetic operations using standard arithmetic operators. However, for more complex mathematical calculations, we need to use the built-in mathematical functions in Visual Basic 2015. There are numerous built-in mathematical functions in Visual Basic 2015. Among them are Abs, Exp, Fix, Int, Rnd, Round, sqrt and more. We shall deal with trigonometric functions and Financial Functions in coming lessons. Most mathematical functions belong to the Math class in Visual Basic 2015. However, not all mathematical functions belong to the Math class.
18.1 The Abs function
In Visual Basic 2015, the Abs function returns the absolute value of a given number.The syntax is
Math. Abs (Number)
Example 18.1
In this example, we shall add a text box control for the user to input his or her number and a label control to display the absolute value of the number. We need to use the Val function to convert text to a numeric value. Rename the textbox as TxtNum and the label as LblAbs.
The Code
Private Sub BtnComp_Click(sender As Object, e As EventArgs) Handles BtnComp.Click
LblAbs.Text = Math.Abs(Val(TxtNum.Text))
End Sub
The output
18.2 The Exp function
In Visual Basic 2015, the Exp function returns the exponential value of a given number. For example, Exp(1)=e=2.71828182
The syntax is
Math.Exp (Number)
Example 18.2
In this example, we shall add a text box control for the user to input his or her number and a label control to display the exponential value of the number. Rename the textbox as TxtNum and the label as LblAbs.
The Code
Private Sub BtnComp_Click(sender As Object, e As EventArgs) Handles BtnComp.Click
LblExp.Text = Math.Exp(Val(TxtNum.Text))
End Sub
* We use the Val function to convert a string to numeric value