» Help

How to check if a string is a number


Checking if a string is a number is quite simple, all we have to do is check if all it's characters are numbers. The situation becomes more complicated when you wish to recognize symbols ('+', '-', '$' etc.), but I'll leave that out for now. This code uses PChar as the parameter for speed reasons. If you want to send a string value to the function just typecast it.

function IsNumber(pcString: PChar): Boolean;
  Result := False;
  while pcString^ <> #0 do // 0 indicates the end of a PChar string
    if not (pcString^ in ['0'..'9']) then
  Result := True;
See an extended version of this routine – IsNumeric – in the Code Snippets Database. This routine can optionally check for floating point numbers.
Author: Unknown
Added: 2007-06-02
Last updated: 2013-10-12

« Return to contents »