Page 1 of 1 1
Topic Options
#115761 - 2004-03-09 06:50 PM ISNUMERIC() - Trying to build the VB-equivalent
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
I am sure there is a cleaner way to do this..

Code:

CLS
BREAK ON
$v1=1
$v2='test'

ISNUMERIC($v1)
ISNUMERIC($v2)

FUNCTION ISNUMERIC($val)
DIM $i
FOR EACH $i IN SPLIT('1 2 3 4 5 6 7 8 9 0')
IF INSTR($val,$i)
?$val
?'Integer check OK'
EXIT 1
ELSE
?$val
?'Integer check not OK'
ENDIF
NEXT
ENDFUNCTION



Comments, suggestions are welcome.

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#115762 - 2004-03-09 07:09 PM Re: ISNUMERIC() - Trying to build the VB-equivalent
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
never mind..

here it is..
Code:

FUNCTION ISNUMERIC($val)
DIM $i
IF CINT($val)
?$val
?'Integer check OK'
ELSE
?$val
?'Integer check not OK'
ENDIF
ENDFUNCTION



Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#115763 - 2004-03-10 10:10 AM Re: ISNUMERIC() - Trying to build the VB-equivalent
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Not quite right, for a start this will determine that "0" is non-numeric.

Also, this will return true if the value is "123%n23".

CInt() will convert as much of the string to numeric as it can, and return it, silently ignoring the non-numeric parts.

A more accurate check might be:
Code:
If CStr($val)=CStr(CInt($val))



But you would have to deal with "" != "0", and things like "0001" would be deemed to be non-numeric.

Top
#115764 - 2004-03-10 10:27 AM Re: ISNUMERIC() - Trying to build the VB-equivalent
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
This is a far better proposition:
Code:
Function udfIsInt($i)
$udfIsInt=0
While $i
If Not InStr("0123456789",Left($i,1)) Exit 1 EndIf
$i=SubStr($i,2)
Loop
$udfIsInt=1
Exit 0
EndFunction



This will return true for "" and "0001", but will return false for "123x456".

Note, you can very simply convert this to a general "check that only these values are present" function by making the "valid" string an additional optional parameter.

Top
#115765 - 2004-03-10 02:17 PM Re: ISNUMERIC() - Trying to build the VB-equivalent
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Richard,

I appreciate your response on this.

Please see the detail in the request to remove the thread - Please remove ISNUMERIC() UDF

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 515 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.037 seconds in which 0.017 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org