Board index » delphi » Format integer with leading zero

Format integer with leading zero

Hi:

Format integer with leading zero only allow 16 character length. e.g
Format('%.116d', [1])        OK
Format('%.17d', [1])        Not working

If the prec is longer then 16, it will not working.

Any suggestion?

Thanks

Roland Zhang

 

Re:Format integer with leading zero


Divide your number x into two parts, x1:=x div 10000; x2:=x mod 10000 and
format them as you wish.

"Roland" <rolandelNONS...@usa.net> p1e v diskusnm p?spvku
news:3cbe5abf_1@dnews...

Quote
> Hi:

> Format integer with leading zero only allow 16 character length. e.g
> Format('%.116d', [1])        OK
> Format('%.17d', [1])        Not working

> If the prec is longer then 16, it will not working.

> Any suggestion?

> Thanks

> Roland Zhang

Re:Format integer with leading zero


Function StrZeroInt(Number : integer;Len : integer) : string;
var
  i,tot : Integer;
  temp : string;
begin
  temp := IntToStr(Number);
  tot := len -length(temp);
  for i := 1 to tot do
    temp := '0'+temp;
  result:=temp;
end;

Re:Format integer with leading zero


The precision length is the integer variable passd into the function.

function GetLeadingZeroString(len, value : integer) : string;
begin
  Result := Format('%.'+IntToStr(len)+'d', [value]);
end;

I am not clear your way is efficient. Is there any better way?

Thanks

Roland Zhang

Quote
vlastik <bar...@spsmvbr.cz> wrote in message news:3cbe7272_2@dnews...
> Divide your number x into two parts, x1:=x div 10000; x2:=x mod 10000 and
> format them as you wish.

> "Roland" <rolandelNONS...@usa.net> p1e v diskusnm p?spvku
> news:3cbe5abf_1@dnews...
> > Hi:

> > Format integer with leading zero only allow 16 character length. e.g
> > Format('%.116d', [1])        OK
> > Format('%.17d', [1])        Not working

> > If the prec is longer then 16, it will not working.

> > Any suggestion?

> > Thanks

> > Roland Zhang

Re:Format integer with leading zero


Thanks

Is there any better way?

Roland Zhang

Quote
Marc Convents <marc.convents@nospam_progen.be> wrote in message

news:3cbeceeb_1@dnews...
Quote
> Function StrZeroInt(Number : integer;Len : integer) : string;
> var
>   i,tot : Integer;
>   temp : string;
> begin
>   temp := IntToStr(Number);
>   tot := len -length(temp);
>   for i := 1 to tot do
>     temp := '0'+temp;
>   result:=temp;
> end;

Re:Format integer with leading zero


Quote
Roland wrote in message <3cbf5d21_2@dnews>...
>The precision length is the integer variable passd into the function.

>function GetLeadingZeroString(len, value : integer) : string;
>begin
>  Result := Format('%.'+IntToStr(len)+'d', [value]);
>end;

>I am not clear your way is efficient. Is there any better way?

There is always better way:
  Result := Format('%.*d', [len,value]);

--
Robert

Other Threads