RepeatUntil
RepeatUntil

Reputation: 2330

Delphi - increment integer start with zeros

How i can increase an integer start with zeros 00000000, 00000001 , 00000002 ... etc.

var
  i:Integer;
begin
  i := 00000000;
  Inc(i);
  ShowMessage(IntToStr(i));

The problem is the inc procedure will trim the zeros on the left.

Output:

1

Not 00000001.

Upvotes: -1

Views: 3004

Answers (2)

Dmitry Beloshistov
Dmitry Beloshistov

Reputation: 11

Try this - only one step after Format() -

for i := 1 to NeedLeadingZeros do begin if (Result[i] <> ' ') then break; Result[i] := '0'; end;

Upvotes: 1

kwarunek
kwarunek

Reputation: 12587

As @Sir Rufo wrote it is only matter of formatting and probably you are looking for SysUtils.Format

// SysUtils should be in uses;
...
const
  DesiredLen = 8;
var
  i:Integer;
begin
  i := 0;
  Inc(i);
  ShowMessage(SysUtils.Format('%.*d', [DesiredLen, i])) 

Upvotes: 12

Related Questions