-
Jan 25th, 2008, 05:22 AM
#1
SIMPLEST IsNumber() function
Function to test an Unsign Whole Number: (cannot be simpler)
Code:
Function IsDigits(ByVal Num As Variant) As Boolean
IsDigits = (Len(Num) > 0) And (Num Like String(Len(Num), "#"))
End Function
'Simplest' IsNumer() function for all possible combination cases: Please test and comment.
You need to change to match with your local setting such as . , $
Code:
Function IsNumber(var As Variant, Optional AllowDot As Boolean, _
Optional AllowComma As Boolean, _
Optional AllowSgnL As Boolean, _
Optional AllowSgnR As Boolean, _
Optional AllowPars As Boolean, _
Optional AllowCurr As Boolean, _
Optional AllowSpcL As Boolean, _
Optional AllowSpcR As Boolean, _
Optional AllowSci As Boolean, _
Optional AllowHex As Boolean, _
Optional AllowOct As Boolean) As Boolean
If Not IsNumeric(var) Then
ElseIf AllowDot > -UBound(Split(var, ".")) Then '-- see Note below
ElseIf Not AllowComma And var Like "*,*" Then '-- thousand seperator
ElseIf Not AllowSgnL And var Like "[-+]*" Then '-- leading sign
ElseIf Not AllowSgnR And var Like "*[-+]" Then '-- trailing sign
ElseIf Not AllowPars And var Like "*(*" Then '-- parentheses as negative
ElseIf Not AllowCurr And var Like "*$*" Then '-- currency
ElseIf Not AllowSpcL And var Like " *" Then '-- leading spaces
ElseIf Not AllowSpcR And var Like "* " Then '-- trailing spaces
ElseIf Not AllowSci And var Like "*[DE]*" Then '-- Scientific
ElseIf Not AllowHex And var Like "*H*" Then '-- Hex
ElseIf Not AllowOct And var Like "*O*" Then '-- Oct
Else
IsNumber = True
End If
End Function
Code:
'-- Note: ElseIf AllowDot > -UBound(Split(var, ".")) Then
'-- equivalent with
' Dim d As Integer
' d = UBound(Split(var, ".")) '-- count number of dots
' ... ...
' ElseIf Not AllowDot And d > 0 Then
' ElseIf AllowDot And d > 1 Then
Last edited by anhn; Feb 5th, 2008 at 09:43 PM.
-
Sep 3rd, 2008, 10:08 PM
#2
Frenzied Member
Re: SIMPLEST IsNumber() function
Code:
Function IsDigits(ByVal Num As Variant) As Boolean
IsDigits = (Len(Num) > 0) And (Num Like String(Len(Num), "#"))
End Function
Faster:
Code:
Function IsDigits(ByVal Num As Variant) As Boolean
IsDigits = (LenB(Num) > 0) And (Num Like String(Len(Num), "#"))
End Function
-
Sep 3rd, 2008, 11:52 PM
#3
Re: SIMPLEST IsNumber() function
Originally Posted by Zach_VB6
Code:
Function IsDigits(ByVal Num As Variant) As Boolean
IsDigits = (Len(Num) > 0) And (Num Like String(Len(Num), "#"))
End Function
Faster:
Code:
Function IsDigits(ByVal Num As Variant) As Boolean
IsDigits = (LenB(Num) > 0) And (Num Like String(Len(Num), "#"))
End Function
How faster with a single call? ±1*10-10 second different?.
Have you actually test it?
This is a bit faster if you want:
Code:
Function IsDigits(ByVal Num As Variant) As Boolean
Dim n As Long
n = Len(Num)
IsDigits = (n > 0) And (Num Like String$(n, "#"))
End Function
-
Sep 4th, 2008, 12:27 PM
#4
Re: SIMPLEST IsNumber() function
The biggest problem is use of Variant, I don't see a need for it so:
Code:
' shorter by one character, two times faster
Function IsDigits(ByVal Num As String) As Boolean
If LenB(Num) Then IsDigits = Num Like String$(Len(Num), "#")
End Function
' shorter by three characters but two times slower
Function IsDigits(ByVal Num As String) As Boolean
IsDigits = Num = Format$(Val(Num), String$(Len(Num), "0"))
End Function
-
Sep 4th, 2008, 05:26 PM
#5
Re: SIMPLEST IsNumber() function
Good use of default Boolean value (False) and ByVal String, Merri.
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
|