如何将整数转换为规定位数的字符串
如 i:=123
转换为 000123

解决方案 »

  1.   

    i := 123;
    s := StringOfChar('0', 6 - Length(IntToStr(i))) + IntToStr(i);
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      tt : String;
    begin
      FmtStr(tt, '000%d', [123]);
      showmessage(tt);
    end;
      

  3.   

    var 
     s:string;
     i:integer;
    begin
      i:=123;
      s:='000000000';// 足够多的 0
      s:=s+IntToStr(i);
      //下面语句取字符串最后6位
      s:=copy(s,length(s)-6,6);// s等于'000123'
    end;
      

  4.   

    function LeadZero(const aIntValue, aWidth: Integer): string;
    begin
      Result := Format(Format('%%.%dd', [aWidth]), [aIntValue]);
    end;
      

  5.   

    function LeadZero(const aIntValue: Int64; const aWidth: Integer): string;
    begin
      Result := Format(Format('%%.%dd', [aWidth]), [aIntValue]);
    end;aWidth <= 32