各位大侠
判断在edit1.text中输入的字符串是否是数字
我在网上找了不少函数,如果是负数时,不能正确判断
我想找个准确的函数 本人菜鸟级,又写不出来,
谢谢大家!!
判断在edit1.text中输入的字符串是否是数字
我在网上找了不少函数,如果是负数时,不能正确判断
我想找个准确的函数 本人菜鸟级,又写不出来,
谢谢大家!!
解决方案 »
- delphi制作的一个小程序中嵌入了谷歌地图,显示信息窗口的问题。。。
- 托盘显示隐藏代码
- 请问高手门MEMO控件的基础应用
- quickreport的主从报表如何sum
- sql客户端为什么不能备份数据?
- procedure AppendRecord(const Values: array of const);这个参数是什么类型的数组?
- 生命,是什么??
- 书看的多的帮个忙.
- 调用父窗体的控件值出错
- 我是Delphi初学者,请问,那里可以下载Delphi7的试用版软件?谢谢。
- adoquery2 can't perform this operation on a closet dataset ;
- 卸载时出现:解压缩支持文件时出错:拒绝访问
try
strtofloat(edit3.text);
except
showmessage('bushi');
exit;
end;showmessage('shi');
var
i:integer;
begin
result:=0;
for i:=1 to length(s) do
if s[i]=c then result:=result+1;
end;function IsNumStr(s:string):boolean;
var
i:integer;
begin
result:=true;
if pos(s,'-')>1 then begin result:=false;exit;end;//负号在中间位置则退出
if GetDotCount(s,'.')>1 then begin result:=false;exit;end;//有多个小数点则退出
if GetDotCount(s,'-')>1 then begin result:=false;exit;end;//有多个负数则退出
if (s[1]='.')or(s[length(s)]='.') then begin result:=false;exit;end;//小数点在第一位或最后一位则退出
for i:=1 to length(s) do
if not (s[i] in ['0'..'9','-','.']) then
begin
result:=false;
exit;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if isNumStr(edit1.Text) then
showmessage('都是数字') else showmessage('不都是数字');
end;
num : float;
begin
TryStrToInt(edit1.text, num);
end;
ShowMessage('是数字')
else
ShowMessage('不是数字')
在OnChange里面用
if (KEY in ['0'..'9','-','.']) then
begin
exit;
end Else
Copy(Edit.text,1,Edit.text-1);
这样怎么输入也都 是数字了 !
KEY:=#8
begin
if not TextToFloat(PChar(S), Result, fvExtended) then
ConvertErrorFmt(@SInvalidFloat, [S]);
end;function TryStrToFloat(const S: string; out Value: Extended): Boolean;
begin
Result := TextToFloat(PChar(S), Value, fvExtended);
end;都是调用TextToFloat.所以还是meiqingsong的方法更好
procedure MustEnterNumber(Sender: TObject; MaxDigit, MaxDecimal, MaxValue:
Integer; var Key: char);
var
s: string;
SimuString: string;
P: Integer;
AEdit: TCustomEdit;
begin
if not (Sender is TCustomEdit) then Exit;
AEdit := TCustomEdit(Sender);
if Key in [#13, #8, #9] then Exit;
if not (Key in ['0'..'9', '.', '-']) then
begin
Key := #0;
Exit;
end;
s := AEdit.Text;
//"-"不能出现在第二位
if (Length(s) > 0) and (AEdit.SelStart <> 0) and (Key = '-') then
begin
Key := #0;
Exit;
end;
P := Pos('.', s);
if P > 0 then
begin
if Key = '.' then
begin
Key := #0;
Exit;
end;
end;
P := Pos('-', s);
if P > 0 then
begin
if Key = '-' then
begin
Key := #0;
Exit;
end;
end;
P := Pos(AEdit.SelText, s);
if P > 0 then Delete(s, p, AEdit.SelLength);
SimuString := SimuInsertChar(s, AEdit.SelStart, Key);
if (SimuString = '-') then
begin
if ((Length(SimuString) > MaxDigit) or
((Length(SimuString) - Pos('.', SimuString) > MaxDecimal) and (Pos('.',
Simustring) <> 0))) then
begin
Key := #0;
Exit;
end;
end
else
begin
if ((Length(SimuString) > MaxDigit) or (StrToFloat(SimuString) > MaxValue)
or
((Length(SimuString) - Pos('.', SimuString) > MaxDecimal) and (Pos('.',
Simustring) <> 0))) then
begin
Key := #0;
Exit;
end;
end;
end;