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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is it Numeric? 3

Status
Not open for further replies.

sanders720

Programmer
Aug 2, 2001
421
US
Can someone please tell me what I am doing wrong here? I simply want to get the first characters of a string until they become non-numeric and apply the quantity variable Q.

Thanks in advance for the help!


Public Sub IsItNumeric(ByVal ConStr As String)
' Example Data is 12XXX or 12
Dim x As Int16
For x = 1 To ConStr.Length
MsgBox(x)
Try
' IsNumeric(ConStr.Substring(1, x))
Q = Convert.ToInt16(ConStr.Substring(1, x))
MsgBox(Q)

Catch ex As Exception
Q = ConStr.Substring(1, x)
MsgBox(Q)

End Try

Next

End Sub
 
Code:
Public function IsItNumeric(ByVal ConStr As String) as integer
    ' Example Data is 12XXX or 12
    dim returnVal as integer = 0
    Dim x As Int16
    For x = 1 To ConStr.Length
        if not IsNumeric(ConStr.Substring(x, 1)) then
            returnVal = Convert.ToInt16(ConStr.Substring(1, x-1))
            Exit For
        end if
    Next

    return returnVal
End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Try
If IsNumeric(ConStr.Substring(1, x)) Then
Q = Convert.ToInt16(ConStr.Substring(1, x))
MsgBox(Q)
Else
Exit Sub
End If


Catch ex As Exception

Throw(ex.Message)
End Try
 
Hmm wont
dim q as integer = integer.parse(ConStr)
throw exception if ConStr is not an integer?
 
IsNumeric vs Not IsNumeric?

This code produces System.OutofRangeException. The input variable is 12XXX, and I am looking for an int16 of 12 to assign to global variable Q.

Public Sub IsItNumeric(ByVal ConStr As String)
' Example Data is 12XXX or 12
Dim x As Int16
For x = 1 To ConStr.Length
If IsNumeric(ConStr.Substring(x, 1)) Then
Q = Convert.ToInt16(ConStr.Substring(1, x - 1))
Exit For
End If
Next

End Sub
 
Anybody notice that the subscript is starting at 1 instead of 0.

Public Sub IsItNumeric(ByVal ConStr As String)
' Example Data is 12XXX or 12
Dim x As Int16
For x = 0 To ConStr.Length - 1


- free online Compare/Diff of snippets
 
My bust, I was thinking it opperated like the old Val command.

Back to my previous post, this should work for '12' as well as '12xxx'

Code:
    Public Function IsItNumeric(ByVal ConStr As String) As Integer
      ' Example Data is 12XXX or 12
      Dim returnVal As Integer = 0
      Dim i As Int16
      Dim sTemp As String = ""
      For i = 1 To ConStr.Length
        If Not IsNumeric(ConStr.Substring(i, 1)) Then
          sTemp = ConStr.Substring(1, i - 1)
          Exit For
        End If
      Next
      'check if all characters are numeric
      If sTemp = "" Then sTemp = ConStr

      returnVal = Convert.ToInt16(sTemp)
      Return returnVal
    End Function

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Why are we using Not IsNumeric instead of simply IsNumeric?

In my original example, I sought to loop thru four times. If 12XXX were the text, the first time would produce a 1 and would be numeric. The seccond would produce a 12 and also would be numeric. The third would produce 12X, and would not be numeric. So, I want to use 12 for variable Q.

Is there only an option for Not IsNumeric? Thanks for the help.

Public Sub IsItNumeric(ByVal ConStr As String)
' Example Data is 12XXX or 12
Dim x As Int16
For x = 0 To ConStr.Length -1
If IsNumeric(ConStr.Substring(1, x)) Then
Q = Convert.ToInt16(ConStr.Substring(1, x - 1))
Exit For
End If
Next

End Sub
 
No you would have to do this

Q should be a string and you should keep on appending until you get to the nonnumeric and then you should convert Q to int
example

Q =Q + ConStr.Substring(1, x - 1)


and when you exit for do this
Dim TotalQ As Integer = Convert.ToInt16(Q)



 
Yes, but X cannot equal 0

If the string equals 12XXX then 0 would start before the 1 in 12XXX when I assign variable Q. Do I need to use Option Base 1.

Secondly, should IsNumeric work on a string value, like

IsNumeric "12" = True
IsNumeric "12X" = False?

Thanks for your reply


' Example Data is 12XXX or 12
Dim x As Int16
For x = 1 To ConStr.Length
If IsNumeric(ConStr.Substring(1, x)) Then
Q = Convert.ToInt16(ConStr.Substring(1, x))
Else
Q = Convert.ToInt16(ConStr.Substring(1, x - 1))
Exit For
End If
Next
MsgBox(Q)
 
Also... "12e4" passes the IsNumeric test.

(Hint: Think scientific notation)

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
aha nice catch
so we have to do this
Dim sIntegerString as String="1234567890"
and the see if your character lies in this string
if it does then numeric else exit for

:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top