Introduction to Numeric Functions in vb.net

Tutorials PanelPosted by

The .NET Framework has too many numeric functions that can be used out of the box for numeric manipulation. This article is to learn about the most common ones that each developer should know.  

The Numeric Functions in VB.NET 

All numeric functions that are available VB.NET are implemented as the constant and static methods of Math Class, which is inherited from the Object class. For using any of Math methods from the Math class, you should use the methods with the word “Math.”, or import the whole class using the keyword “imports” in VB.NET, and “using” in C#. Few of these functions and their usage and overload list are mentioned below:

Abs(int): This method returns about absolute value of particular number.

Dim x As Integer 
x = -2 
Console.WriteLine(Abs(x)) 

Output

2

Overload List

Abs(Decimal)
Abs(Double)
Abs(Int16)
Abs(Int32)
Abs(Int64)
Abs(SByte)
Abs(Single)  

Ceiling(Double): This function returns the smallest integral value that is greater than or equal to a Number.

Dim x As Double
x = 2.56
Console.WriteLine(Ceiling(x)) 

Output

3

Overload List

Ceiling(Decimal)
Ceiling(Double)

Floor(Double): This function returns the largest integer value that is less than or equal to a numeric value.

Dim x As Double 
x = 2.56 
Console.WriteLine(Floor(x))

Output

2

Overload List

Floor(Decimal)
Floor(Double)  

Log(Double): This function returns the base e logarithm of double value. 

Dim x As Double 
x = 10 
Console.WriteLine(Log(x))

Output

2.30258509299405

Overload List

Log(Double)
Log(Double, Double)  Note: the 2nd Double argument is for the log base.

Log10(Double):  This function returns the base 10 logarithm of double value.

Dim x As Double 
x = 10 
Console.WriteLine(Log10(x))

Output

1

Min(int,int): This function compares two numbers and returns the smallest one.

Dim x, y As Integer
x = -2
y = 7
Console.WriteLine(Min(x, y))

Output

2

Overload List

Min(Byte, Byte)
Min(Decimal, Decimal)
Min(Double, Double)
Min(Int16, Int16)
Min(Int32, Int32)
Min(Int64, Int64)
Min(SByte, SByte)
Min(Single, Single)
Min(UInt16, UInt16)
Min(UInt32, UInt32)
Min(UInt64, UInt64)

Max(int,int): The function compares two  numbers and returns the biggest one. 

Dim x, y As Integer
x = -2
y = 7
Console.WriteLine(Max(x, y))

Output

7

Overload List

Max(Byte, Byte)
Max(Decimal, Decimal)
Max(Double, Double)
Max(Int16, Int16)
Max(Int32, Int32)
Max(Int64, Int64)
Max(SByte, SByte)
Max(Single, Single)
Max(UInt16, UInt16)
Max(UInt32, UInt32)
Max(UInt64, UInt64)

Round(int): This function rounds a number to the nearest integral value. 

Dim x As Integer
x = 2.6
Console.WriteLine(Round(x))

Output

3

Overload List

Round(Decimal)
Round(Decimal, Int32)
Round(Decimal, Int32, MidpointRounding)
Round(Decimal, MidpointRounding)
Round(Double)
Round(Double, Int32)
Round(Double, Int32, MidpointRounding)
Round(Double, MidpointRounding)

Pow(Double,Double): The function returns the particular number which is raised to a particular power. For example, if the parameter are a and b, the output will also return back a^b. 

Dim x, y As Double
x = 2
y = 3
Console.WriteLine(Pow(x, y))

Output

8

Sqrt(Double): The function returns square root of a particular number. 

Dim x As Double
x = 25
Console.WriteLine(Sqrt(x))

Output

5

Related Articles:

Leave a Reply

Your email address will not be published. Required fields are marked *