Float2ZStr Routines
Converts a Float into a string of length Len with Zero Padding to the Left.

Unit
QESBPCSConvert

Overloaded Variants
Function Float2ZStr(const X: Extended; const Len: Byte; const Decimals: Byte = 4): string;
Function Float2ZStr(const X: Double; const Len: Byte; const Decimals: Byte = 4): string;
Function Float2ZStr(const X: Single; const Len: Byte; const Decimals: Byte = 4): string;

Declaration
Function Float2ZStr(const X: Extended; const Len: Byte; const Decimals: Byte = 4): 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 an Empty string, where Zero is dependent upon ESBTolerance. Also see Float2EStr, Float2CStr & Float2Str

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.
Decimals is the desired number of Decimal places, defaults to 4

Category
String/Float Conversion Routines

Implementation

function Float2ZStr (const X: Extended; const Len: Byte;
     const Decimals: Byte = 4): string;
var
     Hold: Boolean;
     Len2: Byte;
begin
     if ESBBlankWhenZero and FloatIsZero (X) then
     begin
          Result := BlankStr (Len);
          Exit;
     end;

     Hold := ESBNumPosSign;
     try
          ESBNumPosSign := False;
          Result := Float2EStr (Abs (X), Decimals);
     finally
          ESBNumPosSign := Hold;
     end;

     if (Len > 0) and (FloatIsNegative (X) or (FloatIsPositive (X) 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 FloatIsNegative (X) then // Add Sign if necessary
          Result := '-' + Result
     else if FloatIsPositive (X) and ESBNumPosSign then
          Result := '+' + Result;
End;

Declaration
Function Float2ZStr(const X: Double; const Len: Byte; const Decimals: Byte = 4): string;

Implementation

function Float2ZStr (const X: Double; const Len: Byte;
     const Decimals: Byte = 4): string;
var
     Hold: Boolean;
     Len2: Byte;
begin
     if ESBBlankWhenZero and FloatIsZero (X) then
     begin
          Result := BlankStr (Len);
          Exit;
     end;

     Hold := ESBNumPosSign;
     try
          ESBNumPosSign := False;
          Result := Float2EStr (Abs (X), Decimals);
     finally
          ESBNumPosSign := Hold;
     end;

     if (Len > 0) and (FloatIsNegative (X) or (FloatIsPositive (X) 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 FloatIsNegative (X) then // Add Sign if necessary
          Result := '-' + Result
     else if FloatIsPositive (X) and ESBNumPosSign then
          Result := '+' + Result;
End;

Declaration
Function Float2ZStr(const X: Single; const Len: Byte; const Decimals: Byte = 4): string;

Implementation

function Float2ZStr (const X: Single; const Len: Byte;
     const Decimals: Byte = 4): string;
var
     Hold: Boolean;
     Len2: Byte;
begin
     if ESBBlankWhenZero and FloatIsZero (X) then
     begin
          Result := BlankStr (Len);
          Exit;
     end;

     Hold := ESBNumPosSign;
     try
          ESBNumPosSign := False;
          Result := Float2EStr (Abs (X), Decimals);
     finally
          ESBNumPosSign := Hold;
     end;

     if (Len > 0) and (FloatIsNegative (X) or (FloatIsPositive (X) 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 FloatIsNegative (X) then // Add Sign if necessary
          Result := '-' + Result
     else if FloatIsPositive (X) and ESBNumPosSign then
          Result := '+' + Result;
End;


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