• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2554
  • Last Modified:

How To Convert a VB.NET String To a Long

Hello Experts -

Does anyone know how to convert a string to a long in VB.NET?

This is the most confused language I've ever work with.

1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you KNOW the value in the String always has a VALID Long in it, then:
    Dim i As Long = CLng(yourStringVariableHere)

Open in new window

Otherwise, if you need to validate, then use:
        Dim i As Long
        If Long.TryParse(yourStringVariableHere, i) Then
            ' ... do something with "i" ...
            MessageBox.Show(yourStringVariableHere, "Invalid Long")
        End If

Open in new window

In addition to CLng() there is also Val().

Where as CLng requires the string to be purely numeric (i.e. a valid number), Val() doesn't blow up if the string contains non-numerics.

Val() skips leading white spaces, then begins converting the string into a number up until the point it encounters something that no longer looks like a number.  It returns a Double (so it works with floating point numbers as well).

Val("123ABC") = 123
Val("1.2.3") = 1.2 (2nd decimal point is no longer a valid number)
Val("ABC123") = 0
Val("1,234.56") = 1

Notice that last one... the comma doesn't look numeric to Val, so it stops conversion at the comma separator.  For situations like this, I've written my own subroutine  

The other thing I've done before is to write my own function if I knew the string might have embedded characters or spaces:
    Private Function StripToInt(ByVal Data As String)
        Dim NegativeSign As Boolean = False
        Dim Ch As Char
        For i = 1 To Len(Data)
            Ch = Mid$(Data, i, 1)
            If "0" <= Ch And Ch <= "9" Then
                StripToInt = StripToInt & Ch
            ElseIf Ch = "-" Then
                If Len(StripToInt) = 0 Then
                    NegativeSign = True
                End If
            End If
        If NegativeSign And Len(StripToInt) Then
            StripToInt = "-" & StripToInt
        End If
    End Function

Open in new window

The function simply reads through your string, and strips everything out that might not be a string.  You can then feed the results through CLng() (or integrate the CLng inside the function and have it return a Long rather than a string).
gbmcneilAuthor Commented:
This entire topic is far too complicated for what should be a very simple function.

Hats off to Microsoft for such a marvelous solution!

Rather than spend a week fooling with it, I am going to make the "converted to" value a double (i.e.,) -

LessrPrice = System.Convert.ToDouble(".21")

That way, I don't have to get involved with multiple lines of code.

Thanks to both of you for your help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now