Announcement

Collapse
No announcement yet.

Is number

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

  • Pierre Bellisle
    replied
    >...improvements by Pierre...

    'twas easy, I only added a little block on a well made foundation. :-)

    Pierre

    Leave a comment:


  • Pierre Bellisle
    replied
    Hey Mike,
    Once started, we could also accept a plus sign prefix...
    Code:
     
    FUNCTION isNumeric2(BYVAL Answer AS STRING) AS LONG
      Answer = TRIM$(Answer) 'Trim spaces, may be required in right-justified text for example
      Answer = LEFT$(Answer, INSTR(Answer, ANY "+-")) & _        'In case of space after the sign,
               LTRIM$(MID$(Answer, INSTR(Answer, ANY "+-") + 1)) ' "- 123" for example
      'PRINT Answer & "<"
      IF (LEN(Answer) = 0)                   OR _  'Exclude null string
         (VERIFY (Answer , ".-+0123456789")) OR _  'Accept only valid characters
         (INSTR(-1, Answer, "+") > 1)        OR _  'Plus must be on digit's left
         (INSTR(-1, Answer, "-") > 1)        OR _  'Minus must be on digit's left
         (RIGHT$(Answer, 1) = ".")           OR _  'Must not end with a dot
         (TALLY(Answer, ".") > 1)            THEN  'No more than one dot
         EXIT FUNCTION  'Return zero, not a number
      END IF
      FUNCTION = -1 'Its a number
    END FUNCTION
    Last edited by Pierre Bellisle; 6 Nov 2015, 08:33 PM.

    Leave a comment:


  • Mike Doty
    replied
    The TRIM$ isn't enough?
    I see, allow spaces after minus sign.
    Might add BYVAL Answer AS STRING since input string is being modified.
    Thank you, will incorporate.


    Good improvements by Pierre, below in post # 24.
    Last edited by Mike Doty; 6 Nov 2015, 11:53 PM.

    Leave a comment:


  • Pierre Bellisle
    replied
    If right-justified text is accepted,
    then because it is often shown with the minus sign at the beginning, like this:
    Code:
    - 123
    -  12
    -   1
    I would add
    Code:
    Answer = LEFT$(Answer, INSTR(Answer, "-")) & _
    LTRIM$(MID$(Answer, INSTR(Answer, "-") + 1))
    before the IF line.

    Pierre

    Leave a comment:


  • Mike Doty
    replied
    Here is a solution.
    If a tests fails, add an OR statement and please let us know. No scientific notation.
    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

    Leave a comment:


  • Michael Mattias
    replied
    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.

    Leave a comment:


  • Dale Yarker
    replied
    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,

    Leave a comment:


  • Michael Mattias
    replied
    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

    Leave a comment:


  • Bern Ertl
    replied
    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.

    Leave a comment:


  • Mike Doty
    replied
    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

    Leave a comment:


  • Eric Pearson
    replied
    Or 01.

    Leave a comment:


  • Pierre Bellisle
    replied
    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

    Leave a comment:


  • Bern Ertl
    replied
    Code:
    sText = "1234567"
    IF sText = FORMAT$( VAL( sText), 18) THEN PRINT "ITS A NUMBER!"

    Leave a comment:


  • Mike Doty
    replied
    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, 11:28 PM.

    Leave a comment:


  • Gary Beene
    replied
    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, 10:48 PM.

    Leave a comment:


  • David Clarke
    replied
    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")

    Leave a comment:


  • Mike Doty
    replied
    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

    Leave a comment:


  • Guy Dombrowski
    replied
    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

    Leave a comment:


  • Pierre Bellisle
    replied
    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

    Leave a comment:


  • Michael Mattias
    replied
    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, 06:45 PM.

    Leave a comment:

Working...
X