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

The utility below will test a string to see if it will convert to a valid longint. The utility assumes the number is formatted for base 10.

If (True)
   If (False)
      Begin SQL
         /*
          Name: STR_IsLongint(string_t)->bool
          Path: STR_IsLongint

          Purpose: Test if a string is a longint

          $1 - TEXT - The candidate string
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_BOOLEAN($0;$IsLongint_B)
   C_TEXT($Num_T;$1)
    //--------------------------------------------------------------------------------
    //method_wide_constants_declarations
    //--------------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L)
   C_REAL($Real_R)

End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
$IsLongint_B:=False
$Num_T:=$1
$Real_R:=Num($Num_T)

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

Case of
   : ($Real_R>MAXLONG) //or 2^31-1
   : ($Real_R<-(MAXLONG+1)) //or-2^31
   : (Match regex("^[-+]?\\d+[eE]?.[+-]?.[\\d]*$";$Num_T;1))
Else
   $IsLongint_B:=True
End case

//======================== Clean up and Exit =================================

$0:=$IsLongint_B