dcsimg
Results 1 to 5 of 5

Thread: SIMPLEST IsNumber() function

  1. #1

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Lightbulb 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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    Baby Steps a guided tour
    IsDigits() and IsNumber() functions Wichmann-Hill Random() function >> and << functions for VB CopyFileByChunk

  2. #2
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,071

    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

  3. #3

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: SIMPLEST IsNumber() function

    Quote 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
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    Baby Steps a guided tour
    IsDigits() and IsNumber() functions Wichmann-Hill Random() function >> and << functions for VB CopyFileByChunk

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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

  5. #5

    Thread Starter
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: SIMPLEST IsNumber() function

    Good use of default Boolean value (False) and ByVal String, Merri.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    Baby Steps a guided tour
    IsDigits() and IsNumber() functions Wichmann-Hill Random() function >> and << functions for VB CopyFileByChunk

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width