Int2ZStr Routines
Converts an Integer into a string of length Len with Zero Padding to the Left.

Unit
QESBPCSConvert

Overloaded Variants
Function Int2ZStr(const L: LongInt; const Len: Byte): string;
Function Int2ZStr(const L: Int64; const Len: Byte): string;

Declaration
Function Int2ZStr(const L: LongInt; const Len: Byte): string;

Description
ESBNumPosSign controls whether a '+' Sign appears at the beginning for positive Integers. ESBBlankWhenZero can be set to True to have Zero returned as a string of blanks.

Parameters
Value to Convert to String.
Len is the length of the resultant string. If it is too small then valid digits will be truncated from the right.

Category
String/Integer Conversion Routines

Implementation

function Int2ZStr (const L: LongInt; const Len: Byte): string;
var
     Len2: Byte;
begin
     if ESBBlankWhenZero and (L = 0) then
     begin
          Result := BlankStr (Len);
          Exit;
     end;

     try
          FmtStr (Result, '%d', [abs (L)]); // Format the string
          if L = Low (LongInt) then
               Result := RightAfterStr (Result, 1);
          if (Len > 0) and ((L < 0) or ((L > 0) and ESBNumPosSign)) then
               Len2 := Len - 1 // Need to leave space for the sign
          else
               Len2 := Len;
          Result := PadChLeftStr (LeftStr (Result, Len2), '0', Len2); // Pad with Zeroes
          if L < 0 then // Add Sign if necessary
               Result := '-' + Result
          else if (L > 0) and ESBNumPosSign then
               Result := '+' + Result;
     except
          Result := '';
     end;
End;

Declaration
Function Int2ZStr(const L: Int64; const Len: Byte): string;

Implementation

function Int2ZStr (const L: Int64; const Len: Byte): string;
var
     Len2: Byte;
begin
     if ESBBlankWhenZero and (L = 0) then
     begin
          Result := BlankStr (Len);
          Exit;
     end;

     try
          FmtStr (Result, '%d', [abs (L)]); // Format the string
          if L = Low (Int64) then
               Result := RightAfterStr (Result, 1);
          if (Len > 0) and ((L < 0) or ((L > 0) and ESBNumPosSign)) then
               Len2 := Len - 1 // Need to leave space for the sign
          else
               Len2 := Len;
          Result := PadChLeftStr (LeftStr (Result, Len2), '0', Len2); // Pad with Zeroes
          if L < 0 then // Add Sign if necessary
               Result := '-' + Result
          else if (L > 0) and ESBNumPosSign then
               Result := '+' + Result;
     except
          Result := '';
     end;
End;


HTML generated by Time2HELP
http://www.time2help.com