Home >>VB.Net Built-in Functions >VB.Net program to demonstrate the CDec() function

VB.Net program to demonstrate the CDec() function

Write a VB.Net program to demonstrate the CDec() function

The function CDec() is used to convert the value of different data types to a decimal type.

Syntax :

CDec(val)

Parameters :

  • val: It may be a variable with different types of data.

Return Value :

A converted decimal number will be returned by the CDec() function.

Program :

Below is the source code to demonstrate the CDec() function. The program given is compiled and successfully executed.


Module Module1
    Sub Main()
        Dim num As Decimal = 0
        Dim n1 As Single = 10.25
        Dim n2 As Integer = 12
        Dim n3 As Double = 25.35
        Dim n4 As String = "122"
        num = CDec(n1)
        Console.WriteLine("Decimal Number: {0}", num)
        num = CDec(n2)
        Console.WriteLine("Decimal Number: {0}", num)
        num = CDec(n3)
        Console.WriteLine("Decimal Number: {0}", num)
        num = CDec(n4)
        Console.WriteLine("Decimal Number: {0}", num)
    End Sub
End Module
	
Output:
Decimal Number: 10.25
Decimal Number: 12
Decimal Number: 25.35
Decimal Number: 122

Explanation:

We created a Module1 module in the above program, which contains the Main() method. Five variables num, n1, n2, n3, and n4 have been created in the Main() method and are initialized with 0, 10.25, 12, 25.35, and 122.

num = CDec(n1)
Console.WriteLine("Decimal Number: {0}", num)

num = CDec(n2)
Console.WriteLine("Decimal Number: {0}", num)

num = CDec(n3)
Console.WriteLine("Decimal Number: {0}", num)

num = CDec(n4)
Console.WriteLine("Decimal Number: {0}", num)

We converted the value of the specified variable to a decimal number in the code above and printed it on the console screen.


No Sidebar ads