Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is Numeric question

Status
Not open for further replies.

jrig

Technical User
Sep 5, 2006
36
US
IsNumeric(123+) returns true, while
IsNumeric(1+23) returns false

Seems like a bug to me, can anyone provide insight why this is? Is there a better way determine that a string contains all numbers without looping through each character? TIA-
 
Here is a VB6 article regarding this topic. While many regard VB6 as an inferior language, my point in bringing this up is that the concepts discussed in this article are relevant to your question.

faq222-5901

Please forgive me if I have inadvertently offended you by pointing to a VB6 article.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thanks for the replies and the link. Sirius, I should've tested the code before I put it up, this demonstrates it a little better-
Code:
    Dim wha As String = "123+"
        Dim huh As String = "12+3"
        MsgBox(IsNumeric(wha) & " " & IsNumeric(huh))
"wha" comes back true, and I'm thinking it should not...comments? Think I need an 'IsInteger' function?
 
Try this...

Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wha As String = "123+"
        Dim huh As String = "12+3"
        Dim varInt As String = "12345"
        Dim varNumber As String = "123.45"

        MsgBox("IsNumeric:" & IsNumeric(wha) & " " & IsNumeric(huh) & " " & IsNumeric(varInt) & " " & IsNumeric(varNumber))

        MsgBox("IsInteger:" & IsInteger(wha) & " " & IsInteger(huh) & " " & IsInteger(varInt) & " " & IsInteger(varNumber))
        MsgBox("IsNumber:" & IsNumber(wha) & " " & IsNumber(huh) & " " & IsNumber(varInt) & " " & IsNumber(varNumber))

    End Sub

[blue]    Private Function IsInteger(ByVal Data As String) As Boolean

        IsInteger = IsNumeric(Data & ".0e0")

    End Function[/blue]

[green]    Private Function IsNumber(ByVal Data As String) As Boolean

        IsNumber = IsNumeric(Data & "e0")

    End Function[/green]

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
It probably behaves like this because

CType("123+",Integer) = 123
CType("123+",Single) = 123.0

BUT

CType("1+23",Integer) Throws an Exception
CType("1+23",Single) Throws an Exception

Senior Software Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top