» Help

Code Snippets Database

Control Box
 
 

Requested snippet

This page displays the requested IsNumeric snippet.

You can choose a category, perform a search or generate a Pascal unit containing the snippet by using the Control Box.

IsNumeric
Checks if a string is a numeric value. If AllowFloat is true then the string may contain a floating point number, otherwise it must be an integer. If TrimWhiteSpace is True any white space surrounding Value is trimmed before testing.
function IsNumeric(Value: string; const AllowFloat: Boolean;
  const TrimWhiteSpace: Boolean = True): Boolean;
var
  ValueInt: Int64;      // dummy integer value
  ValueFloat: Extended; // dummy float value
begin
  if TrimWhiteSpace then
    Value := SysUtils.Trim(Value);
  // Check for valid integer
  Result := SysUtils.TryStrToInt64(Value, ValueInt);
  if not Result and AllowFloat then
    // Wasn't valid as integer, try float
    Result := SysUtils.TryStrToFloat(Value, ValueFloat);
end;
Kind of Snippet: Routine
Required units: SysUtils.
Required snippets: None.
See also: None.
Supported Compilers:
 D2   D3   D4   D5   D6   D7  D2005 D2006 D2007 D2009 D2010  DXE  DXE2 Free
Pascal
Red LED Red LED Red LED Red LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Grey LED Green LED
Floating point numbers can be in normal or scientific notation. Integers can be expressed as decimal or hexadecimal numbers. Hex numbers must be prefixed by either '$' or '0x'.