Gallery
Software
Music
Programming
Archive
Wednesday, Apr 24 Week 17
const
NULL = #0; //Blank char
TNumbs = ['0'..'9']; //Set of numerical chars

function isNumeric(p:pAnsiChar):boolean;
var n:longWord;
begin
result:=false;if p=nil then exit; //Check input
if p^='-' then inc(p); //Ignore minus
if p^=NULL then exit; //Deny empty input
while p^NULL do //Loop chars
if p^ in TNumbs then inc(p) else exit; //Check if numerical char
result:=true
end;

function isNumericFloat(p:pAnsiChar):boolean;
var bl:boolean;
begin
result:=false;if p=nil then exit; //Check input
if p^='-' then inc(p); //Ignore minus
if p^=NULL then exit; //Deny empty input
bl:=false; //True if had comma or dot
while p^NULL do begin //Loop all chars
if not (p^ in TNumbs) then //Check if numerical char
if p^ in ['.',','] then begin //Check if comma or dot
if not bl then bl:=true else exit //Deny multiple comma or dot
end else exit; //Deny any other char
inc(p); //Next char
end;
result:=true
end;

Example

procedure TForm1.FormCreate(Sender: TObject);
var s:ansiString;
begin
s:='-27,64';
if isNumericFloat(pAnsiChar(s)) then s:='yes' else s:='no';
caption:=s //Show the result in form caption
end;

Related pages of Check if a string is numerical

Map numbers based on range
Bytes and data-types
Delphi examples
Delphi programming
Programming