VBA Functions: CSng and CDbl

The VBA CSng and CDbl functions convert a numeric value to a floating-point number.

Usage:

CSng(value)

CDbl(value)


Example of Usage

Using the CSng function to convert different types of numeric values (in this case, the CDbl function returns the same results):

Sub example()

    MsgBox CSng("1.245") 'Returns: 1.245
    MsgBox CSng(1.245) 'Returns: 1.245
    MsgBox CSng("45") 'Returns: 45
    MsgBox CSng(" 45.25 ") 'Returns: 45.25
    MsgBox CSng("-147.2347") 'Returns: -147.2347
    MsgBox CSng(True) 'Returns: -1
    MsgBox CSng(False) 'Returns: 0
    
End Sub

The difference between the two functions is that the CDbl function (Double type) allows for greater precision than the CSng function (Single type):

Sub example()

    number = WorksheetFunction.Pi
    
    MsgBox CSng(number) 'Returns: 3.141593
    MsgBox CDbl(number) 'Returns: 3.14159265358979
    
End Sub