function isNum(str:string):boolean;
var i:integer;
begin
result := false;
if str='' then
exit;
result := true;
for i := 1 to length(str)
if str not in ['0'..'9'] then
begin
result := false;
break;
end;
end;
补充一点
function isNum(str:string):boolean;
var i:integer;
begin
result := false;
if str='' then
exit;
result := true;
for i := 1 to length(str)
if str not in ['.','0'..'9'] then //加个'.'
begin
result := false;
break;
end;
end;
function isNumber(const aStr: String): Boolean;
var
I : Integer;
S : String;
begin
Result := True;
I := 1;
S := Trim(aStr);
//判别符号
if S <> '' and (S[1] in ['+', '-']) then Inc(I);
//整数部分
while (I < Length(S)) and (S in ['0'..'9']) do
begin
Inc(I);
end;
//有小数点
if (I < Length(S)) and (S = '.') then
begin
Inc(I);
while (I < Length(S)) and (S in ['0'..'9']) do
begin
Inc(I);
end;
if I < Length(S) then Result := False;//遇到了非数字字符
end
else if (I < Length(S)) then //遇到了非数字字符
begin
Result := False;
end;
end;
tseug的方法我一直很喜欢,我加一个语句:
……
//有小数点
if (i < Length(s)) and (S = '.') then
begin
Inc(i);
if s = '.' then Dec(i); //加在这里,防止数字后两个小数点而不报错,如12..
while (i < Length(s)) and (s in ['0'..'9']) do
Inc(i);
if i < Length(s) then result := false; //遇到了非数字字符
end
……
if (i < Length(s)) and (S = '.') then
begin
Inc(i);
if s = '.' then Dec(i); //加在这里,防止数字后两个小数点而不报错,如12..
while (i < Length(s)) and (s in ['0'..'9']) do
Inc(i);
if i < Length(s) then result := false; //遇到了非数字字符
end