如何判断一个字符串是不是一个Float或integer类型?(10分)

S

snappy

Unregistered / Unconfirmed
GUEST, unregistred user!
也就是说如何判断一个字符串中除数字和小数点外有无其它字符.
 
try
strtofloat(edit1.text);
application.messagebox('成功!','提示',0);
except
application.messagebox('有非法字符!','提示',0);
end;
 
try
StrToInt(edit1.text);
application.messagebox('整数!','提示',0);
except
try
StrToInt(edit1.text);
application.messagebox('浮点数!','提示',0);
except
application.messagebox('有非法字符!','提示',0);
end;
end;
 
给你一个函数,用于判断
function IsNum(str:AnsiString):boolean;//判断str是否为数值,是数值返回真,否则返回假
var
i,j,len:integer;//len纪录str的长度,j用于纪录小数点的个数
begin
len:=Length(str);
j:=0;
for i := 1 to len do
if(str in ['0'..'9','.']) then//一个十进制数是由0~9这些字符组成的
begin
if(str='.') then inc(j);
if(j=0) or (j=1) then//没有小数点是整数,一个小数点是实数,多余两个小数点的不是数值
result:=true
else
result:=false;
end
else
result:=false;
end;
 
多人接受答案了。
 
顶部