Results 1 to 13 of 13

Thread: Counting digits, extracting numbers (resolved)

  1. #1

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526

    Counting digits, extracting numbers (resolved)

    I can get the number of digits in an integer in two functions:

    Digits = Len(Str(intX)) - 1

    But is there a function that does this in one?



    also . . .



    I have various strings like:

    1 day
    2 days
    14 days
    365 days

    And I want to extract just the numbers into another string. Is this the slickest way:

    strNumber = Replace(strDays, " day", vbNullString)
    strNumber = Replace(strDays, "s", vbNullString)



    I know I'm quibbling when it could be said, "if it ain't broke... " but I'm just as concerned with creating streamlined code that's easy to read.
    Last edited by mikeycorn; Feb 20th, 2003 at 09:43 PM.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  2. #2
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    check out the "val" function

  3. #3

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    Val("21 days") won't do the trick.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  4. #4

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    Oh my God! It certainly does! (I honestly just assumed that was going to be a 0 returned there, thanks.)
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141
    Val will search until it hits a space. Try both of these
    VB Code:
    1. MsgBox Val("21 Days")
    2. MsgBox Val("day 21")

  6. #6

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    Aha. Interesting.

    I'm embarrassed for not having tried it myself but I just kind of assumed you had to strip all the text out before using Val or it'd just be = 0.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  7. #7
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    There is no single function in VB that tells you the number of digits in an integer that i know of... But IMO you should be using Cstr and tehre is no need for the -1

    VB Code:
    1. Dim X As Integer
    2. X = 1111
    3. MsgBox Len(CStr(X))
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  8. #8

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    Thanks, Arc. I read one time long ago to stay away from Variants if at all possible and it always stuck with me. Perhaps I should read up a bit more on them.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  9. #9
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    Why do you say that? were you using a variant?.. If so then you could just use Len(MyInt) and it would return the result.
    Last edited by Arc; Feb 20th, 2003 at 09:23 PM.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  10. #10
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Well, this function may work better if there is no space between the number and the rest of the string:
    VB Code:
    1. Public Function NumbersOnly(Expression As Variant) As Long
    2.  Dim x As String
    3.  x = Expression
    4.  For i = 1 To 47
    5.   x = Replace(x, Chr(i), "")
    6.  Next
    7.  For i = 58 To 255
    8.   x = Replace(x, Chr(i), "")
    9.  Next
    10.  NumbersOnly = CLng(x)
    11. End Function
    It will remove EVERYTHING but the numbers themselves from the
    string.
    Luke

  11. #11

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    That's like the nuclear option. Very thorough indeed! Put that one in the code bank.
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  12. #12

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    More in the same vein:

    Here's what I want to do:

    a$="9x99"

    If a$ = val(a$) then print "all numbers"

    Of course, what I'm getting is a type-mismatch.

    So, what's the best way to see if a string is all numbers (can I avoid a loop?)
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

  13. #13

    Thread Starter
    Fanatic Member mikeycorn's Avatar
    Join Date
    Jun 2000
    Location
    Aliso Viejo, CA, USA
    Posts
    526
    Hey Mikey, try IsNumeric.

    Hey, thanks Mikey!
    ~ mikeycorn

    With over 45,000 Questions in the User Submitted Database, it's the Most Popular Quiz Creation Software on the Net:

    PEST - The Personal Exam Self-Tester

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