May 02, 2024, 12:19:41 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Validate a Numeric in a STRING

Started by billhsln, March 29, 2012, 11:10:14 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

Is there a function to make sure that a numeric value in a STRING variable is a valid numeric?  I have looked thru the doc and searched here, but have not found any thing of use.  Did I miss it?

Thanks,
Bill
When all else fails, get a bigger hammer.

ZeroDog

hmmm... well, if the numeric value is expected to be anything other than 0, you could use the VAL function to determine if it's a numeric expression or not... if its not a numeric expression, VAL will return 0.

LarryMc

Quote from: ZeroDog on March 29, 2012, 11:27:31 PM
...you could use the VAL function to determine if it's a numeric expression or not... if its not a numeric expression, VAL will return 0.
Not exactly true; for example

n="123m456"
print val(n)

doesn't return 0; it returns 123.00 (with normal precision.)

indicating it is not a valid number.
and you can add all the test you want
but using the following sub it will return 0

n="123m456"
print isNumeric(n)

sub isNumeric(n:string),int
   n=ltrim$(n)
num = 1
for i = 0 to len(n)-1
if instr("0123456789",mid$(n,i,1))=0
num=0
breakfor
endif
next i
return num
endsub


If you want to add negatives and commas like
if instr("0123456789-,",mid$(n,i,1))=0
and then add code to see if the - and/or the , (s) are in the correct places.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

ZeroDog

Ah, correct you are.  Nice little snippet, I might just have to steal that one from you.  ;)

LarryMc

Another approach that might work, depending on your data.

n="-123456"
print isNumeric2(n)

n="123-456"
print isNumeric2(n)

sub isNumeric2(n:string),int
int n1,num
num = 1
n=ltrim$(rtrim$(n))
n1 = val(n)
if n<>ltrim$(str$(n1))
num=0
endif
return num
endsub


This allows a - sign provided it is in the correct location.

LarryMc
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

billhsln

This seems to work with for my needs.  I hope some one else will find this of use.

SUB IsNumeric(InString:STRING),INT

DEF NotNumeric = False, IsNumeric = True, i, length:INT
DEF IsDecimal = False, CaughtDecimal = False:INT
DEF IsSignPresent = False, IsSignAllowed = True, CountNumeric = 0:INT
DEF c:STRING

NotNumeric = False
IsNumeric = True

InString = LTRIM$(RTRIM$(InString))
length = LEN(InString)

FOR i = 1 TO length
c = MID$(InString,i,1)
SELECT c
CASE "0"
CASE& "1"
CASE& "2"
CASE& "3"
CASE& "4"
CASE& "5"
CASE& "6"
CASE& "7"
CASE& "8"
CASE& "9"
CountNumeric++
IsSignAllowed = False
CASE "."
IF CaughtDecimal OR CountNumeric = 0
RETURN False
ENDIF
CountNumeric = 0
CaughtDecimal = True
IsDecimal = True
CASE "-"
CASE& "+"
IF IsSignPresent OR NOT IsSignAllowed
RETURN False
ENDIF
IsSignPresent = True
DEFAULT
RETURN False
ENDSELECT
NEXT i

IF CountNumeric = 0
RETURN False
ENDIF
RETURN True
ENDSUB


Bill
When all else fails, get a bigger hammer.