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

VB.Net program to demonstrate the CSng() function

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

The function CSng() is used to convert the values of different data types into a single type.

Syntax :

CSng(val)

Parameters :

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

Return Value :

A converted single number will be returned by the CSng() function.

Program :

Below is the source code for demonstrating the CSng() function. The program given is compiled and successfully executed.


Module Module1
    Sub Main()
        Dim num As Single = 0
        Dim n1 As Double = 10.25
        Dim n2 As Integer = 12
        Dim n3 As Decimal = 25.35
        Dim n4 As String = "122"
        num = CSng(n1)
        Console.WriteLine("Single Number: {0}", num)
        num = CSng(n2)
        Console.WriteLine("Single Number: {0}", num)
        num = CSng(n3)
        Console.WriteLine("Single Number: {0}", num)
        num = CSng(n4)
        Console.WriteLine("Single Number: {0}", num)
    End Sub
End Module
	
Output:
Single Number: 10.25
Single Number: 12
Single Number: 25.35
Single 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 = CSng(n1)
Console.WriteLine("Single Number: {0}", num)

num = CSng(n2)
Console.WriteLine("Single Number: {0}", num)

num = CSng(n3)
Console.WriteLine("Single Number: {0}", num)

num = CSng(n4)
Console.WriteLine("Single Number: {0}", num)

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