c里面的atoi函数可以自动截取相应的数字组成整形或浮点型,自动忽略数字后的非数字,delphi里有类似的函数吗?  

解决方案 »

  1.   

    StrToIntDef('123ABC', 0);
    我想结果并不是楼主所想要的
      

  2.   

    var
      S: string;
      LResult,
      ErrorIndex: Integer;
    begin
      S := '123ABC';
      Val(S, LResult, ErrorIndex);
      ShowMessage(Format('转化结果:%D, 出错位置:%D', [LResult, ErrorIndex]));
    end;
      

  3.   

    var
      S: string;
      LResult:single;
      ErrorIndex: Integer;
    begin
      S := '123.03ABC';
      Val(S, LResult, ErrorIndex);
      ShowMessage(Format('转化结果:%f, 出错位置:%D', [LResult, ErrorIndex]));
    end;我把LResult设为single,但运行后LResult的结果是12303,而不是123.03
      

  4.   

    自己写算了,比如:function MyStrToFloat(const s:String):Double;
    var i:Integer;
        t:String;
    begin
      t:='';
      for i:=1 to length(s) do
      begin
        if not (s[i] in [#46,#48..#57]) then
        begin
          t:=copy(s,1,i-1);
          break;
        end;
      end;
      if t='' then t:=s;
      Result:=StrToFloatDef(t,0);
    end;
      

  5.   


    function MyStrToFloat(const s:String):Double;
    var i:Integer;
        t:String;
    begin
      t:='';
      for i:=1 to length(s) do
      begin
        if not (s[i] in [#46,#48..#57]) then
        begin
          t:=copy(s,1,i-1);
          break;
        end;
      end;
      if t='' then t:=s;
      Result:=StrToFloatDef(t,0);
    end;