Announcement

Collapse
No announcement yet.

Is number

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is number

    Many years ago there was a function in the language I was using that would return true or false if the string was a number:

    Code:
    MY_STRING = "1234567"
    IF MY_STRING IS NBR THEN PRINT "ITS A NUMBER!"
    Aside from rolling my own is there a way to do this now in PB / WIN?

  • #2
    Take a look at RETAIN$.
    There are no atheists in a fox hole or the morning of a math test.
    If my flag offends you, I'll help you pack.

    Comment


    • #3
      I should have kept the author's name.
      Code:
      FUNCTION isNumeric(Answer AS STRING) AS LONG
        Answer = TRIM$(Answer) 'may be required if right-justified text
        IF (LEN(Answer) = 0)                  OR _
           (VERIFY (Answer , ".-0123456789")) OR _
           (RIGHT$(Answer,1) = ".")           OR _
           (INSTR(-1,Answer,"-") > 1)         OR _
           (TALLY(Answer,".") > 1) THEN
           EXIT FUNCTION  'exit, return 0 not-numeric
        END IF
        FUNCTION = -1 'numeric
      END FUNCTION
      https://duckduckgo.com instead of google

      Comment


      • #4
        RETAIN$ would be a lot simpler and shorter. Just RETAIN$ what you want then check the length of the original string vs. processed string. If they are the same, then it's a number. If not....
        There are no atheists in a fox hole or the morning of a math test.
        If my flag offends you, I'll help you pack.

        Comment


        • #5
          Code:
          Function NonNegativeWholeNumber(ByVal a As String) As Long      ' Boolean
           Local i As Long
          
           'exclude strings such as 0000004
           Local b As String                   '
           b = LTrim$(a,"0")                   '
           If a <> "0" And a <> b Then         '
            Function = 0                       '
            Exit Function                      '
           End If                              '
           
           For i = 1 To Len(a)
            If Asc(a, i) < 48 Or Asc(a, i) > 57 Then Exit For
           Next
           Function = (i > Len(a))
          End Function
          Politically incorrect signatures about immigration patriots are forbidden. Googling “immigration patriots” is forbidden. Thinking about Googling ... well, don’t even think about it.

          Comment


          • #6
            Many years ago there was a function in the language I was using that would return true or false if the string was a number:
            COBOL?

            Code:
               IF VARNAME [is] {NUMERIC|ALPHABETIC|ALPHANUMERIC}   [THEN] 
                    it is what you tested
            (Both "IS" and "THEN" here are optional, treated as documentary)

            NOTE: NUMERIC = consists ONLY of the digit characters "0" thru "9;" ALPHABETIC = only "a-z" or "A-Z" ; ALPHANUMERIC = both
            Last edited by Michael Mattias; 5 Nov 2015, 05:45 PM.

            Comment


            • #7
              Hey David,

              First thing for me would be to know what could possibly be in the analysed string.
              Can it have more than one dot ".".
              Can it have one or more than one comma ",".
              Can it have a leading plus sign "+".
              Etc, etc...

              isNumeric("-") Says it's a number
              NonNegativeWholeNumber("00") Says it's not a number
              IF LEN("-0..1-99." ) = LEN(RETAIN$("-0..1-99." ,ANY "-.0123456789")) THEN... Says "
              -0..1-99." is a number

              Pierre

              Comment


              • #8
                Simple test using PB command VERIFY

                Originally posted by David Clarke View Post
                Many years ago there was a function in the language I was using that would return true or false if the string was a number:

                Code:
                MY_STRING = "1234567"
                IF MY_STRING IS NBR THEN PRINT "ITS A NUMBER!"
                Aside from rolling my own is there a way to do this now in PB / WIN?
                Code:
                 IF VERIFY (test$,"0123456789.+-") THEN 
                    print"Test string is not all numeric": beep
                 else 
                    print"Test string is all numeric" 
                 end if
                Old QB45 Programmer

                Comment


                • #9
                  Guy,
                  Same question at:
                  http://www.powerbasic.com/support/pb...ad.php?t=43051
                  Code:
                  FUNCTION PBMAIN () AS LONG
                   LOCAL test AS STRING
                   test$ = "...+++---"
                   IF VERIFY (test$,"0123456789.+-") THEN
                      ? "Test string is not all numeric": BEEP
                   ELSE
                      ? "Test string is all numeric"
                   END IF
                  END FUNCTION
                  https://duckduckgo.com instead of google

                  Comment


                  • #10
                    WE HAVE A WINNER!!


                    VERIFY function

                    Purpose:
                    Determine whether each character of a is present in another string.

                    Syntax
                    x = VERIFY([start&,] MainString, MatchString)

                    Remarks
                    VERIFY returns zero if each character in MainString is present in MatchString. If not, it returns the position of the first non-matching character in MainString.

                    This function is very useful for determining if a string contains only digits, for example.

                    VERIFY is case-sensitive, so capitalization matters.

                    Restrictions
                    If start& evaluates to a position outside of the string on either side, or if start& is zero, VERIFY returns zero.

                    See also
                    INSTR, LCASE$, LTRIM$, MID$, REMOVE$, REPLACE, RIGHT$, RTRIM$, TALLY, TRIM$, UCASE$

                    Example
                    ' returns 4 since "." is not in "0123456789"

                    x& = VERIFY("123.65,22.5", "0123456789")



                    ' returns 7 since 5 starts it past the first non-digit ("." at position 4)

                    x& = VERIFY(5,"123.65,22.5", "0123456789")

                    Comment


                    • #11
                      Hey David,

                      I'm not sure how Verify would be used to test this string (per Pierre's suggestion)...

                      Code:
                      22.33.55.66
                      or this one ...

                      Code:
                      .-.-1
                      Testing the order of valid number characters would be part of the routine.
                      Last edited by Gary Beene; 5 Nov 2015, 09:48 PM.

                      Comment


                      • #12
                        See post #3 that uses verify as one of the tests.
                        If you find a condition that isn't numeric add it to the function by adding another OR condition.

                        Post #3
                        Code:
                         FUNCTION isNumeric(Answer AS STRING) AS LONG
                          Answer = TRIM$(Answer) 'may be required if right-justified text
                          IF (LEN(Answer) = 0)                  OR _
                             (VERIFY (Answer , ".-0123456789")) OR _
                             (RIGHT$(Answer,1) = ".")           OR _
                             (INSTR(-1,Answer,"-") > 1)         OR _
                             (TALLY(Answer,".") > 1) THEN
                             EXIT FUNCTION  'exit, return 0 not-numeric
                          END IF
                          FUNCTION = 1
                        END FUNCTION
                        Code:
                        #DIM ALL
                        FUNCTION PBMAIN () AS LONG
                          LOCAL sTitle,MY_STRING AS STRING
                          MY_STRING = "1234567"
                          DO
                            MY_STRING = INPUTBOX$("Type anything",sTitle,MY_STRING)
                            IF LEN(MY_STRING) = 0 THEN EXIT DO
                            IF IsNumeric(MY_STRING) THEN
                              sTitle = "ITS A NUMBER!"
                            ELSE
                              sTitle = "NOT A NUMBER"
                              BEEP
                          END IF
                        LOOP
                        END FUNCTION
                        
                         FUNCTION isNumeric(Answer AS STRING) AS LONG
                          Answer = TRIM$(Answer) 'may be required if right-justified text
                          IF (LEN(Answer) = 0)                  OR _
                             (VERIFY (Answer , ".-0123456789")) OR _
                             (RIGHT$(Answer,1) = ".")           OR _
                             (INSTR(-1,Answer,"-") > 1)         OR _
                             (TALLY(Answer,".") > 1) THEN
                             EXIT FUNCTION  'exit, return 0 not-numeric
                          END IF
                          FUNCTION = 1
                        END FUNCTION
                        Already discussed here: http://www.powerbasic.com/support/pb...ad.php?t=43051
                        Last edited by Mike Doty; 5 Nov 2015, 10:28 PM.
                        https://duckduckgo.com instead of google

                        Comment


                        • #13
                          Code:
                          sText = "1234567"
                          IF sText = FORMAT$( VAL( sText), 18) THEN PRINT "ITS A NUMBER!"
                          Bernard Ertl
                          InterPlan Systems

                          Comment


                          • #14
                            Hi Bern,
                            0.11 or 7.0 won't work.

                            Again, all depends on the string to be tested,
                            is it from a database with a predetermined format
                            or is it from a textbox where a user can type anything,
                            or is it... Etc, etc.

                            Pierre

                            Comment


                            • #15
                              Or 01.
                              "Not my circus, not my monkeys."

                              Comment


                              • #16
                                You guys are quick!
                                Guess I'll stay with what I have (for now.)
                                Bern, this would have been nice.
                                Code:
                                FUNCTION PBMAIN () AS LONG
                                  LOCAL sPrompt,sTitle,sDefault AS STRING
                                  sDefault = "8.0"
                                  DO
                                    sDefault = INPUTBOX$(sPrompt,sTitle,sDefault)
                                    IF LEN(sDefault) = 0 THEN EXIT FUNCTION
                                    IF IsNum(sDefault) THEN sTitle = "ITS A NUMBER!" ELSE sTitle = "INVALID NUBBER"
                                  LOOP
                                END FUNCTION
                                
                                FUNCTION IsNum(sText AS STRING) AS LONG
                                  IF sText = FORMAT$(VAL(sText),18) THEN FUNCTION = 1
                                END FUNCTION
                                https://duckduckgo.com instead of google

                                Comment


                                • #17
                                  I have some data validation routines where I use the simple reformat to process user input. I don't use it to validate so much as to assure that what goes into the database is a number.

                                  sText = FORMAT$( VAL( sText), 18)

                                  If you need a yes/no to "is it a number", it's not going to work in all cases. If forcing the input into the required format is acceptable, that works fine. YMMV.
                                  Bernard Ertl
                                  InterPlan Systems

                                  Comment


                                  • #18
                                    To Test: sText, STrING
                                    Code:
                                    IF VAL (sText) =  VAL (FORMAT$(VAL(sText))) THEN 
                                        it's 'a number'  whatever that means
                                    The question falls back to, what, in context, is a 'number?'

                                    Just for educational purposes, COBOL does allow you to create your own "VERIFY strings" for lack of a perfect analogy..

                                    Code:
                                     
                                      SPECIAL-NAMES. 
                                           CLASS  DIGITSDECIMALSANDSIGNS  IS '0123456789+-.'. 
                                           CLASS  LOWERCASE IS 'a' THRU 'z' . 
                                    
                                    ....
                                    PROCEDURE DIVISION. 
                                    ... 
                                        IF character-var IS DIGITSDECIMALSANDSIGNS [THEN] 
                                              PERFORM whatever 
                                        ELSE  
                                              IF character-var IS LOWERCASE  THEN 
                                                   PERFORM something-else.
                                    MCM

                                    Comment


                                    • #19
                                      Kind of depends on what you're looking for, doesn't it? Signed or unsigned integer, fixed decimal, floating point, scientific notation, etc etc.

                                      Lots of nice code here, but no one piece can fit everyone's need or all requirements.

                                      Cheers guys,
                                      Dale

                                      Comment


                                      • #20
                                        Originally posted by Dale Yarker View Post
                                        Kind of depends on what you're looking for, doesn't it?....no one piece can fit everyone's need or all requirements.
                                        Truer words ne'er were spoken.

                                        Comment

                                        Working...
                                        X