各位大侠 
判断在edit1.text中输入的字符串是否是数字
我在网上找了不少函数,如果是负数时,不能正确判断
我想找个准确的函数 本人菜鸟级,又写不出来,
谢谢大家!!

解决方案 »

  1.   


    try
       strtofloat(edit3.text);
    except
       showmessage('bushi');
       exit;
       end;showmessage('shi');
      

  2.   

    function GetDotCount(s:string;c:char):integer;
    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;
      

  3.   

    var
      num : float;
    begin
      TryStrToInt(edit1.text, num);
        
    end;
      

  4.   

    TryStrToFloat(edit1.text, num); 
      

  5.   

    if(TryStrToFloat(edit1.text, num))then
      ShowMessage('是数字')
    else
      ShowMessage('不是数字')
      

  6.   

    2楼的蛮好 
      在OnChange里面用
        if (KEY in ['0'..'9','-','.']) then 
        begin      
        exit; 
        end Else
       Copy(Edit.text,1,Edit.text-1);
    这样怎么输入也都 是数字了 ! 
     
      

  7.   

      在OnChange里面用     if (KEY in ['0'..'9','-','.']) then 
        KEY:=#8
      

  8.   

    刚才错了应该在Edit1KeyPress里面
      

  9.   

    function StrToFloat(const S: string): Extended;
    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的方法更好
      

  10.   

    贡献一个过程,用在控件的OnKeyPress事件中//限制在编辑框中输入数字
    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;