本人水平有限,如有错误,欢迎指正。
在进行文本输入的时候,我们可以通过KeyPress进行输入字符的限制,以达到只能输出数字或浮点数的限制,但有的时候对于一个字符串变量需要进行类型判断,例如:必须是数字字符串或浮点数字符串,就需要自己写函数进行判断了。
function TForm1.isnum(str:string):boolean;//判断是否全为数字(整数)
var
i:integer;
begin
for i:=1 to length(str) do
if not (str[i] in ['0'..'9']) then
begin
result:=false;
exit;
end;
result:=true;
end;
function TForm1.IsDigit(S: string): Boolean;//判断是否为浮点数字符串
var
i, j: integer;
begin
Result := True;
j := 0;
for i := 1 to length(s) do
begin
if not (s[i] in ['0'..'9', '.']) then
Result := False;
if s[i] = '.' then
j := j + 1;
end;
if j > 1 then
Result := False;
if (s[1] = '.') or (s[length(s)] = '.') then
Result := False;
s := copy(s, 1, pos('.', S) - 1);
j := 0;
for i := 1 to length(s) do
begin
if s[I] = '0' then
j := j + 1;
end;
if j > 1 then
Result := False;
end;