Announcement

Collapse
No announcement yet.

Is number

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

  • José Roca
    replied
    > No one thought of REGEXPR?

    I did, some time ago, but I'm using VBScript regular expressions instead of the built in PB ones.

    I'm using this pattern:

    Pattern: "^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$"

    The initial "^" and the final "$" match the start and the end of the string, to ensure the check spans the whole string.
    The "[\+\-]?" part is the initial plus or minus sign with the "?" multiplier that allows zero or one instance of it.
    The "\d*" is a digit, zero or more times.
    "\.?" is the decimal point, zero or one time.
    The "\d+" part matches a digit one or more times.
    The "(?:[Ee][\+\-]?\d+)?" matches "e+", "e-", "E+" or "E-" followed by one or more digits, with the "?" multiplier that allows zero or one instance of it. (Used to parse numbers like "1.2345678901234567e+029".)

    If somebody wants to translate it to the PB syntax (don't know if it requires changes or not), above you have the explanation of what it does.
    Last edited by José Roca; 16 Oct 2017, 07:21 AM.

    Leave a comment:


  • Stuart McLachlan
    replied
    No one thought of REGEXPR?
    Something like:

    Code:
    #COMPILE EXE
    #DIM ALL
    
    FUNCTION PBMAIN () AS LONG
        DIM strInput AS STRING
    
        strInput = "+.123"
    
        IF IsNumber(strInput) THEN
            MSGBOX "True"
        ELSE
            MSGBOX "False
        END IF
    END FUNCTION
    
    FUNCTION IsNumber(strINput AS STRING) AS LONG
    LOCAL LngResult AS LONG
    REGEXPR "^[-+]?[0-9]*\.?[0-9]+$" IN strInput TO lngResult
    FUNCTION = (LngResult = 1)
    END FUNCTION

    Leave a comment:


  • Dale Yarker
    replied
    Change the "impossible" in my previous post to "virtually impossible". If the code is long enough, and input parameters are added to select options, then a generic IS_NUMERIC is possible. How long are you willing to wait for answer?

    Leave a comment:


  • Dale Yarker
    replied
    "o" is only considered "not non-numeric" when it follows "&", then it means the number is octal. If a number is octal, then neither "8" nor "9" are numeric; they are errors if present in an octal number. VAL checks that.

    "A" to "F" are only numeric if the "number" is HEX, (starts with "&H"). VAL checks that.

    9D9 is format for literal floating point. The "D" stands for "* 10^". For "9D" VAL returns 9; So is the string passed to VAL a number or not? VAL has a problem here being used for IS_NUMERIC.

    "Z" passed to VAL returns 0. But a legit 0 (or 0.0 or &H0000) also returns 0. How to tell a non-numeric 0 return from a numeric 0 return? Second problem for VAL as a IS_NUMERIC detector.

    Throw in the "is decimal point comma or period" from earlier, and I'd say a generic all possibilities IS_NUMERIC function is impossible! You have to code to the set of rules needed in a particular application.


    Leave a comment:


  • Rodney Hicks
    replied
    Yeah, from the looks of things, the OP is going to have to invent a wheel to match the vehicle he's creating.

    Leave a comment:


  • Eric Pearson
    replied
    Clever idea, using the UnusedVar& value, but...

    I'm usually not a fan of VAL because it interprets a ton of non-numeric characters as numeric. For example VAL("9D9") produces "9000000000". These characters...

    01234567890.-+&AaBbCcDdEeFfHhOoQq + CHR$(9,32)

    ...can all be interpreted as "not non-numeric" by VAL. Also, VAL says "9" is not a numeric character if the string starts with &o... lots and lots of pitfalls.

    Leave a comment:


  • Rodney Hicks
    replied
    It seems to me that VAL solves this problem:
    Syntax:
    VAL StrgExpr [, offset] TO ValueVar [, DigitsVar&, UnusedVar&]
    If the Unusedvar$ has content, then StrExpr is not a number even if it has content that have a numeric flavor. The OP wanted to know if it was a number.

    Leave a comment:


  • Jim Robinson
    replied
    I found the IsNumeric routine here some years ago. I'm pretty sure it was something Hutch shared, but it could be someone else... so many contributors here.

    I modified the basic routine to fit some particular needs.
    Code:
    [color=007F00]'--------------------------------------------------------------------------------
    '- String Validataion routines:
    '-  IsAlpha     - returns true if all letters                (A-Z,a-z)
    '-  IsAlphaEX   - returns true if all letters and/or blanks  (A-Z,a-z," ")
    '-  IsCapAlpha  - returns true if all capital letters        (A-Z)
    '-  IsNumeric   - returns true if all numbers                (0-9)
    '-  IsNumericEX - returns true if all numbers, commas, decimal points, plus, minus eg: -1,234.56 = True
    '-  IsZIP       - returns true if all numbers and/or dash    (0-9, "-")
    '-  IsDollars   - returns true is all numbers, commas, decimal points, dollar signs (0-9,",",".","$")
    '-  IsAlphaNumeric - returns true if all numbers and/or letters (A-Z,a-z,0-9)
    
    '--------------------------------------------------------------------------------
    '===============================================================================================
    '// Returns 1 if a string is an alpha char (A-Z,a-z)
    [/color][color=0000C0]FUNCTION [/color][color=000000]IsCapAlpha[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop529[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 65
       ! jl  NotAlpha529  ; < 65? Not alpha
       ! cmp bl, 90
       ! jg  NotAlpha529  ; > 90? Not alpha
    [/color][color=000000]Alpha529[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop529
       ! jmp StartLoop529
    [/color][color=000000]NotAlpha529[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop529[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    FUNCTION [/color][color=000000]IsAlpha[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop530[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 65
       ! jl  NotAlpha530  ; < 65? Not alpha
       ! cmp bl, 122
       ! jg  NotAlpha530  ; > 122? Not alpha
       ! cmp bl, 91
       ! jl  Alpha530     ; < 91? Alpha
       ! cmp bl, 97
       ! jl NotAlpha530   ; < 97? Not alpha
    [/color][color=000000]Alpha530[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop530
       ! jmp StartLoop530
    [/color][color=000000]NotAlpha530[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop530[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    FUNCTION [/color][color=000000]IsAlphaEX[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop530x[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 32       ; a blank is ok too
       ! je  Alpha530x
       ! cmp bl, 65
       ! jl  NotAlpha530x  ; < 65? Not alpha
       ! cmp bl, 122
       ! jg  NotAlpha530x  ; > 122? Not alpha
       ! cmp bl, 91
       ! jl  Alpha530x     ; < 91? Alpha
       ! cmp bl, 97
       ! jl NotAlpha530x   ; < 97? Not alpha
    [/color][color=000000]Alpha530x[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop530x
       ! jmp StartLoop530x
    [/color][color=000000]NotAlpha530x[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop530x[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    [/color][color=007F00]'===============================================================================================
    '// Returns 1 if a string is numeric (0-9)
    [/color][color=0000C0]FUNCTION [/color][color=000000]IsNumeric[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
    
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop531[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 48
       ! jl  NotNumeric531  ; < 48? Not numeric
       ! cmp bl, 57
       ! jg  NotNumeric531  ; > 57? Not numeric
       ! inc eax
       ! cmp eax, ecx
       ! je  EndLoop531
       ! jmp StartLoop531
    [/color][color=000000]NotNumeric531[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop531[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    [/color][color=007F00]'// Returns 1 if a string is numeric (0 through 9, and the Plus (+), Minus(-), and  Decimal (.)
    [/color][color=0000C0]FUNCTION [/color][color=000000]IsNumericEX[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop533[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 43
       ! jl  NotNumeric533   ; lower than minus
       ! cmp bl, 46
       ! jle OKgoon533   ;  is decimal, or comma, or plus, or minus
       ! cmp bl, 48
       ! jl  NotNumeric533  ; < 48? Not numeric
       ! cmp bl, 57
       ! jg  NotNumeric533  ; > 57? Not numeric
    [/color][color=000000]OKgoon533[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop533
       ! jmp StartLoop533
    [/color][color=000000]NotNumeric533[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop533[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    [/color][color=007F00]' like IsNumeric, except allows a DASH
    [/color][color=0000C0]FUNCTION [/color][color=000000]IsZIP[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop534[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 45
       ! je  OKgoon534      ;  is a dash
       ! cmp bl, 48
       ! jl  NotNumeric534  ; < 48? Not numeric
       ! cmp bl, 57
       ! jg  NotNumeric534  ; > 57? Not numeric
    [/color][color=000000]OKgoon534[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop534
       ! jmp StartLoop534
    [/color][color=000000]NotNumeric534[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop534[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    [/color][color=007F00]' like IsNumeric, except allows a commas, decimals, dollar signs
    [/color][color=0000C0]FUNCTION [/color][color=000000]IsDollars[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=000000]sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop535[/color][color=8000FF]:
       [/color][color=C00000]! mov bl, [eax]
       ! cmp bl, 36
       ! je  OKgoon535      ;  is a $
       ! cmp bl, 44
       ! je  OKgoon535      ;  is a ,
       ! cmp bl, 46
       ! je  OKgoon535      ;  is a .
       ! cmp bl, 48
       ! jl  NotNumeric535  ; < 48? Not numeric
       ! cmp bl, 57
       ! jg  NotNumeric535  ; > 57? Not numeric
    [/color][color=000000]OKgoon535[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop535
       ! jmp StartLoop535
    [/color][color=000000]NotNumeric535[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop535[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    
    [/color][color=007F00]'===============================================================================================
    '// Returns 1 if a string is alphanumeric (A-Z,a-z,0-9)
    [/color][color=0000C0]FUNCTION [/color][color=000000]IsAlphanumeric[/color][color=8000FF]([/color][color=000000]s [/color][color=0000C0]AS STRING[/color][color=8000FF]) [/color][color=0000C0]AS LONG
    #REGISTER NONE
    LOCAL [/color][color=000000]sPtr&[/color][color=8000FF], [/color][color=000000]sLen&
       [/color][color=0000C0]ON ERROR RESUME NEXT
       [/color][color=000000]sPtr [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) - [/color][color=000000]1
       sLen& [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF]) + [/color][color=0000C0]LEN[/color][color=8000FF]([/color][color=000000]s[/color][color=8000FF])
       [/color][color=C00000]! mov eax, sPtr ;ptr to string
       ! mov ecx, sLen ;addr: end of string
    [/color][color=000000]StartLoop532[/color][color=8000FF]:
       [/color][color=C00000]! inc eax
       ! cmp eax, ecx
       ! je  EndLoop532
       ! mov bl, [eax]
       ! cmp bl, 65
       ! jl  NotAlpha532   ; < 65? Not alpha
       ! cmp bl, 122
       ! jg  NotNumeric532 ; > 122? Not NotNumeric
       ! cmp bl, 91
       ! jl  Alpha532      ; < 91? Alpha
       ! cmp bl, 97
       ! jl NotAlpha532    ; < 97? Not alpha
    [/color][color=000000]Alpha532[/color][color=8000FF]:
       [/color][color=C00000]! jmp StartLoop532
    [/color][color=000000]NotAlpha532[/color][color=8000FF]:
       [/color][color=C00000]! cmp bl, 48
       ! jl NotNumeric532  ; < 48? Not numeric
       ! cmp bl, 57
       ! jg NotNumeric532  ; > 57? Not numeric
       ! jmp StartLoop532
    [/color][color=000000]NotNumeric532[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]False
       [/color][color=0000C0]EXIT FUNCTION
    [/color][color=000000]EndLoop532[/color][color=8000FF]:
       [/color][color=0000C0]FUNCTION [/color][color=8000FF]= [/color][color=000000]True
    [/color][color=0000C0]END FUNCTION
    
    [/color][color=007F00]'--------------------------------------------------------------------------------
    [/color]

    Leave a comment:


  • Michael Mattias
    replied
    Since value isn't really important, Remove$ can be used to remove spaces, commas and dots, etc before numeric test.
    There's really no difference I can see between
    Code:
        ToTest$ =  REMOVE$(RawString$, some_set_characters_defined_non_numeric)
         CALL testochoice (ToTest$) TO iResult
    AND
    Code:
        ToTest$ =  RETAIN$(RawString$, some_set_characters_defined_are_numeric)
         CALL testochoice (ToTest$) TO iResult
    It's not really in the test, it's in the defining of the allowed characters, their frequency/permitted occurrences, their positions within 'RawString$' and any required or prohibited relationships between and/or among characters.

    The question "Does this string represent a number?" is simply too Insufficient and too incomplete to expect THE ONE TRUE ANSWER.

    Leave a comment:


  • Borje Hagsten
    replied
    Since value isn't really important, Remove$ can be used to remove spaces, commas and dots, etc before numeric test.
    Combination IsCharAlphaNumeric <> 0 and IsCharAlpha = 0 returns following as numeric: 0123456789²³¹
    Try testing 2² (^ should also be sort of numeric, or..?)

    Leave a comment:


  • Eric Pearson
    replied
    > Comma is not numeric.

    https://en.wikipedia.org/wiki/Decima..._decimal_comma

    Leave a comment:


  • Michael Mattias
    replied
    Pretty good but it cannot handle comma
    Comma is not numeric. As Mike Doty says ".. way of looking at comma is that it is not a valid digit, but only for formatting."

    But now (not that it's a 'first' here) a question other than that which was asked is being addressed: it's no longer " does string 'x' represent a number?" but rather it becomes " does string 'x' represent a number or a formatted number?"

    And let me screw up this comma thing (and decimal point thing) forever: in Europe the usage of the comma and the decimal point is reversed from the usage here in the States for formatting numbers.

    Comma usage can be important! Like I saw on a t-shirt worn by one of my classmates at my Silver Sneakers(r) fitness classes:

    {picture of a nice meal}

    Let's eat Grandma.
    Let's eat, Grandma.
    Commas Save Lives!


    MCM
    Last edited by Michael Mattias; 12 Oct 2017, 05:22 PM. Reason: Correct quote tags amd Mike D's given name

    Leave a comment:


  • Chris Chancellor
    replied
    maybe like this ?

    Code:
    #COMPILE EXE
    #DIM ALL
    
    ' Check Numeric.bas
    ' https://forum.powerbasic.com/forum/user-to-user-discussions/programming/61574-is-number/page2
    
    
    FUNCTION PBMAIN () AS LONG
      LOCAL sTitle,MY_STRING, RtString 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, RtString) THEN
          ?  "ITS A NUMBER!" + $CRLF + _
           STR$(VAL(RtSTRING))
        ELSE
            BEEP
          ? "NOT A NUMBER"
    
        END IF
      LOOP
    
    END FUNCTION
    
     FUNCTION oldisNumeric(Answer AS STRING) AS LONG
      Answer = TRIM$(Answer)
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (INSTR(-1,Answer,"-") > 1)         OR _
       (ASC(Answer)=45 AND LEN(Answer)=1) OR _
       (TALLY(Answer,".") > 1) THEN
       EXIT FUNCTION  'exit, return 0 not-numeric
      END IF
      FUNCTION = 1
    END FUNCTION
    
    
    
      FUNCTION isNumeric(BYVAL Answer AS STRING, newAnswer AS STRING) AS LONG
    
      Answer = TRIM$(Answer)
      newAnswer = ""
      'remove all commas if any
      REPLACE "," WITH "" IN answer
      REPLACE " " WITH "" IN answer
    
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (RIGHT$(Answer,1) = "+")           OR _
       (RIGHT$(Answer,1) = "-")           OR _
       (INSTR(-1,Answer,"-") > 1)         OR _
       (INSTR(-1,Answer,"+") > 1)         OR _
       (TALLY(Answer,".") > 1) THEN
       EXIT FUNCTION  'exit, return 0 not-numeric
      END IF
      newAnswer = Answer
      FUNCTION = 1
    END FUNCTION

    Leave a comment:


  • George Bleck
    replied
    regarding commas/decimals make sure you read my post #27 as it is already being lost in the static.

    Leave a comment:


  • Chris Chancellor
    replied
    Mike, maybe allow commas to be entered first into function isNumeric
    Initially check Answer for commas and then remove them and then check test results for numeric

    Maybe need a 2 stage process inside function isNumeric
    first stage --> remove commas to become Answer
    second stage --> test Answer for numeric

    Leave a comment:


  • Mike Doty
    replied
    I hear ya.
    One way of looking at comma is that it is not a valid digit, but only for formatting.

    Using INSTR instead of TALLY broke routine using 1.1 so Tally is back.

    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(BYVAL Answer AS STRING) AS LONG
      Answer = TRIM$(Answer)
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (RIGHT$(Answer,1) = "+")           OR _
       (RIGHT$(Answer,1) = "-")           OR _
       (INSTR(-1,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

    Leave a comment:


  • Chris Chancellor
    replied
    Hello Mike
    Pretty good but it cannot handle comma

    Leave a comment:


  • Mike Doty
    replied
    Found some more to exclude as valid
    Others suggested BYVAL which was missing
    Scientific notation is not considered
    https://forum.powerbasic.com/forum/u...umeric?t=43051
    Scientific notation is not considered

    1+1 returns valid so needs more work

    Code:
    #DIM ALL
    FUNCTION PBMAIN () AS LONG                   'ISNUMBER.BAS  so can find in the future
      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([B]BYVAL[/B] Answer AS STRING) AS LONG
      Answer = TRIM$(Answer)
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (RIGHT$(Answer,1) = "+")           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
    handle 1+1
    Code:
    FUNCTION isNumeric(BYVAL Answer AS STRING) AS LONG
      Answer = TRIM$(Answer)
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (RIGHT$(Answer,1) = "+")           OR _
       (RIGHT$(Answer,1) = "-")           OR _
       (INSTR(-1,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
    Got rid of tally, more consistent, but maybe tally was better

    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(BYVAL Answer AS STRING) AS LONG
      Answer = TRIM$(Answer)
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (RIGHT$(Answer,1) = "+")           OR _
       (RIGHT$(Answer,1) = "-")           OR _
       (INSTR(-1,Answer,".") > 1)         OR _
       (INSTR(-1,Answer,"+") > 1)         OR _
       (INSTR(-1,Answer,"-") >1)   THEN
       EXIT FUNCTION  'exit, return 0 not-numeric
      END IF
      FUNCTION = 1
    END FUNCTION

    Last edited by Mike Doty; 11 Oct 2017, 11:47 AM.

    Leave a comment:


  • George Bleck
    replied
    Just to stir the pot a little but also just so it is said... remember that MANY regions use the comma, period, and space differently to represent the radix and thousands separator characters.

    Whatever function you use needs to be regionally specific OR it would require you code some really complex rulesets to allow all.

    These all could be considered valid numbers...
    • 1234.56
    • 1234,56
    • 1,234.56
    • 1.234,56
    • 1 234.56
    • 1 234,56


    Seeing a number like 12,345 could be VERY confusing depending on context.

    Is it 12 thousand, three hundred forty-five?
    or is it 12 and 345 thousandths?


    Last edited by George Bleck; 11 Oct 2017, 11:38 AM.

    Leave a comment:


  • Mike Doty
    replied
    Verify also needed to check for +
    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)
      IF (LEN(Answer) = 0)                OR _
       (VERIFY (Answer , ".-+0123456789"))OR _
       (RIGHT$(Answer,1) = ".")           OR _
       (INSTR(-1,Answer,"-") > 1)         OR _
       (ASC(Answer)=45 AND LEN(Answer)=1) OR _
       (TALLY(Answer,".") > 1) THEN
       EXIT FUNCTION  'exit, return 0 not-numeric
      END IF
      FUNCTION = 1
    END FUNCTION








    Last edited by Mike Doty; 11 Oct 2017, 11:10 AM.

    Leave a comment:

Working...
X