FoxPro functions which determine if a variable is a character string or a numeric string
Solution 1
You can create your own function like this.
FUNCTION IsAllDigits
LPARAMETERS tcSearched, tcOptionalSearch
* tcSearched = the string of characters to test.
* tcOptionalSearch = optional, additional characters to allow.
LOCAL lcSearch
m.lcSearch = "01234567989" + IIF(VARTYPE(m.tcOptionalSearch) = "C", m.tcOptionalSearch, "")
LOCAL lcRemaining
m.lcRemaining = CHRTRAN(m.tcSearched, m.lcSearch, "")
RETURN ( LEN(m.lcRemaining) = 0 )
ENDFUNC
Solution 2
FUNCTION ISNUMERIC
LPARAMETERS cVal
LOCAL llNumeric, lnLen, lcChr, lnDecs, lnVal
llNumeric = VARTYPE(cVal) = "N" && Donkey has sent a numeric value
lnDecs = 0
DO CASE
CASE llNumeric
CASE VARTYPE(cVal)<>"C" && Not a character
OTHERWISE
cVal = ALLTRIM(cVal) && Trim spaces
lnLen = LEN(cVal) && How many characters
llNumeric = .T. && Assume
i = 0
DO WHILE llNumeric AND i<lnLen
i = i+1
lcChr = SUBSTR(cVal,i,1) && Get next char
lnVal = VAL(lcChr)
DO CASE
CASE lcChr = "0" && Allowed
CASE lnVal>0 && 1 - 9 OK
CASE INLIST(lcChr, "-", "+") && Allowed but ONLY at the start
llNumeric = i = 1
CASE lcChr = "." && Decimal point but ONLY one
lnDecs = lnDecs+1
llNumeric = lnDecs = 1
OTHERWISE
llNumeric = .F.
ENDCASE
ENDDO
ENDCASE
RETURN llNumeric
ENDFUNC
Solution 3
This could work for ISDIGIT() or ISALPHA().
Function IsAllDigits(myValue)
lReturn = .t.
FOR i = 1 TO LEN(myvalue)
IF !ISDIGIT( SUBSTR(myValue, i, 1) )
lReturn = .f.
EXIT
ENDIF
ENDFOR
RETURN lReturn
ENDFUNC
Solution 4
How about a one liner?
Function IsNumeric
Lparameters pString
Return m.pString == Chrtran(m.pString, Chrtran(m.pString, "0123456789", ""), "")
EndFunc
You can any other valid characters to "0123456789" like "." or ","
Solution 5
There is more simple to test if a string is numeric or not :
- If String="123" => val('String')>0
- If String="AB123" => val('String')=0
That's all...
Sithu
A passionate software developer based in Yangon, Myanmar (Burma) since 2005, using various languages and technologies such as PHP, Node.js, Rails, Android, Visual FoxPro, MySQL, PostgreSQL, MongoDB, Git, etc. Also spending much time in management, mentoring, coaching and research. Original creator and core developer at PHPLucidFrame. Check out my open source! profile for Sithu on Stack Exchange, a network of free, community-driven Q&A sites http://stackexchange.com/users/flair/887306.png
Updated on July 09, 2022Comments
-
Sithu almost 2 years
I'm looking for a Visual FoxPro function which is similar to the PHP function is_numeric().
I have found this, but I could not useVARTYPE
orTYPE
because the variable is always a character string which contains digits only.I found
ISDIGIT()
function, but the manual says that it only checks the first character.Determines whether the leftmost character of the specified character expression is a digit (0 through 9).
ISDIGIT(cExpression)
Parameters
cExpressionSpecifies the character expression that ISDIGIT( ) tests. Any characters after the first character in cExpression are ignored.
I would create my own function using the regular expression object
VBScript.RegExp
FUNCTION isNumeric( tcValue ) LOCAL oRE oRE = CreateObject("VBScript.RegExp") oRE.Pattern = '^[0-9]+$' RETURN oRE.test( tcValue ) ENDFUNC ? isNumeric( '123' )
But, is there any function provided by FoxPro for this purpose?
Am I just overlooking?Also same for
ISALHPA()
which determines whether the leftmost character in a character expression is alphabetic. I want to check if the variable contain only alphabets.