KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Code Snippet: Quickly test if a string or text variable is a number
PRODUCT: 4D | VERSION: 12.3 | PLATFORM: Mac & Win
Published On: February 16, 2012

Here is a quick code snippet that can be used to determine if a string or text variable is a number:

C_TEXT($1)
C_BOOLEAN($0)
$0:=($1=String(Num($1)))


In the example above the variable $1 is converted to number using the NUM command (stripping out the non-numbers), then converted back to a string using the STRING command. This is compared to the original value of $1.
  • If they match, there is only numbers in the string.
  • If they do not match, there is a non-number in the string

This is quick and dirty tip can be useful, for example, when validating user input from the REQUEST command.

If the code is saved as a project method named UTIL_StringIsANumber then it could be used like this:

$something:=Request("enter something")
If (UTIL_StringIsANumber($something))
  ALERT("proper number format")
Else
  ALERT("not proper number format")
End if


Note: This comparison method will return false for 00 or 0010 because neither of these are a properly formatted number. For example. you would not normally say 00 or 0010, you would say 0 or 10 (both of which are properly formatted and return true).