KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility to test if a string is a numeric
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: January 24, 2013

The utility below will test a string to see if it will convert to a valid number. The utility tests for hexidecimal as well as base 10.

If (True)
   If (False)
      Begin SQL
         /*
          STR_IsNumeric

          Purpose: test a string to see if it is a number

          $1 - text - Candidate string
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_BOOLEAN($0)
   C_TEXT($Buffer_T;$1)
    //--------------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
   C_TEXT($Hex_T)
   C_BOOLEAN($isHEX_B)
End if
//====================== Initialize and Setup ================================

$Buffer_T:=$1

//======================== Method Actions ==================================

If ($Buffer_T="Ox@")
    // Test for valid hexidecimal
    //
   $Hex_T:=$Buffer_T
   $Hex_T:=Substring($Buffer_T;3)
   For ($Ndx;1;Length($Hex_T))
      $isHEX_B:=(Position($Hex_T[[$Ndx]];"0123456789ABCDEF")>0)
      If (Not($isHEX_B))
         $Ndx:=Length($Hex_T)
      End if
   End for

   $0:=$isHEX_B

Else
    // Test the string to see if it contains non numeric characters
    //
   $0:=($Buffer_T=String(Num(Replace string($Buffer_T;"e";""));"0"*Length($Buffer_T)))

End if