Dave
Dave

Reputation: 387

converting a string to a number

I have a script that receives a string that contains a number.

The string may be "1000" or "1000.25" or "1111111111111111111111111" or "abc111"

I would like to be able to check firstly if it is a number, and fail if it is not, and also fail if there are fractions.

I am having trouble converting to a suitable number type that still lets me check for the mentioned scenarios.

Basically I need to check that the string only contains an integer number, no matter how large that number may be.

Would really appreciate some help on this one.

Thanks

Dave

Upvotes: 1

Views: 10488

Answers (3)

stealthyninja
stealthyninja

Reputation: 10371

This is the function I've used to achieve what you want:

Function is_numeric(ByVal sText)
    Dim iCnt, sChr

    is_numeric = True

    If IsNull(sText) OR sText = "" Then
        is_numeric = False
        Exit Function
    End If

    If sText = "" Then
        is_numeric = False
        Exit Function
    End If

    For iCnt = 1 To Len(sText)
        sChr = Mid(sText, iCnt, 1)
            If Asc(sChr) < 48 OR Asc(sChr) > 57 Then
            is_numeric = False
            Exit For
        End If
    Next
End Function

Upvotes: 0

Emmanuel N
Emmanuel N

Reputation: 7449

Use IsNumeric to check if is a number and then use CInt to convert

Upvotes: 2

Roman Ryltsov
Roman Ryltsov

Reputation: 69734

Did you have a chance to discover CInt and CDbl already?

Upvotes: 0

Related Questions