myStrToFloat

  function myStrToFloat(const myStr: string; var myResult: Real): Boolean;
  var
    myTempStr: string;
    ResultStr: string;
    myChar: Char;
    myPos, myLen: integer;
  begin
    myPos := 1;
    ResultStr := '';
    myTempStr := Trim(myStr);
    myLen := Length(myTempStr);
    while myPos <= myLen do
    begin
      myChar := myTempStr[myPos];
      if not (myChar in ['0'..'9', '.', '+', '-']) then Break;
      if (myChar in ['+', '-']) and (myPos > 1) then Break;
      if (myChar = '.') and (AnsiPos('.', ResultStr) <> 0) then Break;
      ResultStr := ResultStr + myChar;
      myPos := myPos + 1;
    end;
    if ResultStr = '' then
    begin
      myResult := 0.0;
      Result := False;
    end
    else
    begin
      try
        myResult := StrToFloat(ResultStr);
        Result := True;
      except
        myResult := 0.0;
        Result := False;
      end;
    end;
  end;

posted on 2012-08-15 16:20  孤独的流浪汉  阅读(94)  评论(0编辑  收藏  举报

导航