|
-
Feb 20th, 2003, 05:33 PM
#1
Thread Starter
Fanatic Member
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.
-
Feb 20th, 2003, 06:30 PM
#2
PowerPoster
check out the "val" function
-
Feb 20th, 2003, 06:36 PM
#3
Thread Starter
Fanatic Member
Val("21 days") won't do the trick.
-
Feb 20th, 2003, 06:37 PM
#4
Thread Starter
Fanatic Member
Oh my God! It certainly does! (I honestly just assumed that was going to be a 0 returned there, thanks.)
-
Feb 20th, 2003, 06:51 PM
#5
Val will search until it hits a space. Try both of these
VB Code:
MsgBox Val("21 Days")
MsgBox Val("day 21")
-
Feb 20th, 2003, 06:59 PM
#6
Thread Starter
Fanatic Member
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.
-
Feb 20th, 2003, 07:48 PM
#7
PowerPoster
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:
Dim X As Integer
X = 1111
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.

-
Feb 20th, 2003, 08:12 PM
#8
Thread Starter
Fanatic Member
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.
-
Feb 20th, 2003, 09:19 PM
#9
PowerPoster
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.

-
Feb 20th, 2003, 10:40 PM
#10
Frenzied Member
Well, this function may work better if there is no space between the number and the rest of the string:
VB Code:
Public Function NumbersOnly(Expression As Variant) As Long
Dim x As String
x = Expression
For i = 1 To 47
x = Replace(x, Chr(i), "")
Next
For i = 58 To 255
x = Replace(x, Chr(i), "")
Next
NumbersOnly = CLng(x)
End Function
It will remove EVERYTHING but the numbers themselves from the
string.
-
Feb 20th, 2003, 10:46 PM
#11
Thread Starter
Fanatic Member
That's like the nuclear option. Very thorough indeed! Put that one in the code bank.
-
Feb 20th, 2003, 11:00 PM
#12
Thread Starter
Fanatic Member
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?)
-
Feb 20th, 2003, 11:16 PM
#13
Thread Starter
Fanatic Member
Hey Mikey, try IsNumeric.
Hey, thanks Mikey!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|