Results 1 to 7 of 7

Thread: converting to integer

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    72

    Exclamation converting to integer

    hi dear
    can u tell me how can i chane a vribale containg some string to integer form.Actually i have a variable getting result from net and store it in string form,now i have to check if this variable contain a number then i have to change it's format to integer.else if it store some string for eg."program" then i've to set it to 0(zero).so if u know pls help me.

    Thanks n regards.
    Uttam

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: converting to integer

    You can use Val(Expresion). If it's a number it will return that number, else it returns 0 (cero)
    VB Code:
    1. Val("23") = 23
    2.     Val("HI23") = 0

  3. #3
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: converting to integer

    Use IsNumeric

    VB Code:
    1. If IsNumeric(YourValue) Then
    2.   Debug.Print "I got your number bucko"
    3. Else
    4.   MsgBox "Not a number jerk!"
    5. End if

    IsDate is another one you should squirrel away in the recesses of your brain for later use.

  4. #4
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: converting to integer

    VB Code:
    1. Dim n As Integer
    2. n = CInt(strValue)

    you would obviously have to be sure there are only numeric values in that string
    VB Code:
    1. Private Function OnlyNumbers(ByVal s As String) As Boolean
    2.   Dim n As Integer
    3.   Dim bRes As Boolean
    4.   bRes = True
    5.   For n = 1 To Len(s)
    6.     If Not IsNumeric(Mid(s, n, 1)) Then
    7.       bRes = False
    8.       Exit For
    9.     End If
    10.   Next
    11.   OnlyNumbers = bRes
    12. End Function

    Edit: Too Late!!! Damn.

    r0ach™
    Don't forget to rate the post

  5. #5
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: converting to integer

    Quote Originally Posted by jcis
    You can use Val(Expresion). If it's a number it will return that number, else it returns 0 (cero)
    VB Code:
    1. Val("23") = 23
    2.     Val("HI23") = 0
    Ok, but:
    VB Code:
    1. Val("12HI34") = 12

    r0ach™
    Don't forget to rate the post

  6. #6
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: converting to integer

    Quote Originally Posted by r0ach
    Edit: Too Late!!! Damn.
    You got too fancy

  7. #7
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: converting to integer

    Not only would you need to insure the string contains ONLY numerals, you also need to make sure it contains no commas AND that IF it only contains numerals, that the number can be a VALID integer (zero to 32,767).

    The following project I did to test this had one text box to enter your string, one to display whether the string can be converted to an integer and a button to kick off that analysis/conversion.

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Integer
    3. Dim my_int As Integer
    4.  
    5. 'Get rid of any commas in your string
    6. Text1.Text = Replace(Text1.Text, ",", "")
    7.  
    8. 'Since largest integer is 32767 quick exit if string is longer
    9. 'than 5 characters
    10. If Len(Text1.Text) > 5 Then
    11.     Text2.Text = "This string can NOT be converted to an integer."
    12.     Exit Sub
    13. End If
    14.  
    15. ' Check each character in string to see if it's a number, exit if not
    16. ' Ascii value must be 48 to 57 to be a numeral
    17. For i = 1 To Len(Text1.Text)
    18.     If Asc(Mid(Text1.Text, i, 1)) < 48 Or Asc(Mid(Text1.Text, i, 1)) > 57 Then
    19.         Text2.Text = "This string can NOT be converted to an integer."
    20.         Exit Sub
    21.     End If
    22. Next i
    23.  
    24. ' Check value of numeral to see if is 32767 or less
    25. If Val(Text1.Text) >= 0 And Val(Text1.Text) <= 32767 Then
    26.     Text2.Text = "This string CAN be converted to an integer."
    27.     my_int = CInt(Text1.Text)
    28.     MsgBox "String has been converted to an integer.", vbOKOnly, "Integer Conversion"
    29.     Exit Sub
    30. Else
    31.     Text2.Text = "This string can NOT be converted to an integer."
    32. End If
    33.  
    34. End Sub
    Do canibals not eat clowns because they taste funny?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width