Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format %d 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I am trying to use the format function to take:

4245

and pad it so it appears like:

00000004245

I have tried using the Format function with the %d arguments, but I'm apparently not using it correctly.

Could someone give me an example of how to correctly format this information?

Thanks!

leslie
 

Code:
sResultString := Format('%11.11d',[nInputValue]);
 
well, I thought I was going to be thrilled, but that didn't work! Here's what I've got. I'm forced to create a HUGE flat file (1116 character flat file!). The payment amount (the last element) needs to be left padded with 0, but it's not working. Can you see what I'm doing wrong?

Thanks!

leslie

Code:
unit SHARETransfer;

interface

uses DB, dateUtils, SysUtils, Classes, Forms, IniFiles, Dialogs;

procedure CreateSHAREFile(PaymentInfo : TDataSet; AppDate : string);


implementation


procedure CreateSHAREFile(PaymentInfo : TDataSet; AppDate : string);
var
RecordString, SeqNum : string;
LastTransmitDate : TDateTime;
FileInfo : TStrings;
Shareini : TIniFile;
i, newSeq : integer;
const
RecordStringFormat = '%-220s%-20s%-5s%-5s%-30s%-287s%-10s%-151s%-30s%-1s%-80s%-22s%-10s%-20s%-5s%-141s%-10s%-4s%-6s%-25s%27.27d';
begin
  Shareini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'JMS.ini');
  if Shareini.ReadDate('TransmittalInfo', 'LastTransmittalDate', 0) <> Today then
  begin
    Shareini.WriteString('TransmittalInfo', 'LastTransmitSequence ', '000');
    Shareini.WriteDate('TransmittalInfo', 'LastTransmittalDate', Today);
  end;


  SeqNum := Shareini.ReadString('TransmittalInfo', 'LastTransmitSequence ', '0');
  newSeq := strToint(SeqNum);
  inc(newSeq);
  SeqNum := '00' + intToStr(NewSeq);
  Shareini.WriteString('TransmittalInfo', 'LastTransmitSequence ', SeqNum);

  if SeqNum <> '0' then
  begin
    FileInfo := TStringList.Create;
    i := 1;
    with PaymentINfo do
    begin
      while not eof do
      begin
        RecordString := Format(RecordStringformat, [Trim(FieldByName('STREET1').AsString + ' ' + FieldByName('STREET2').AsString), '', '21801', '21801', FieldByName('CITY').AsString, '', FormatDateTime('mmddyyyy', Today), '', '21801_' + FieldByName('PANELID').Asstring, '', Trim(FieldByname('FirstName').AsString + ' ' + FieldByName('LASTNAME').AsString), '', FormatDateTime('mmddyyyy', Today), '21801' + FormatDateTime('mmddyyyy', Today) + SeqNum, intToStr(i), '', FormatDateTime('mmddyyyy', Today), '', FieldByName('STATE').AsString, '', (FieldByName('TOTALPAY').Asinteger * 100)]);
        FileINfo.Add(RecordString);
        next;
        inc(i);
      end;
    end;

    FileINfo.SaveToFile(ExtractFilePath(Application.ExeName) + 'PreviousTransfers\Payment_' + formatdateTime('mmddyy', Today) + SeqNum + '21801' + '.txt');
    FileINfo.Free;
  end
  else
    ShowMessage('Error with Transmittal Sequence Number, please contact IT.');
end;
end.
 

[tt]FieldByName('TOTALPAY').Asinteger * 100[/tt]

looks suspicious. Multiplying by 100 would seem to say that you have a decimal value in the TOTALPAY field. In that case perhaps something like

[tt]Round(FieldByName('TOTALPAY').AsFloat * 100)[/tt]

might produce the result you want.


I don't know if the length of 27 is too big for a %d field. You may need to scale back and put in your own constant of a few zeroes in one field and use a smaller value for the size of the TOTALPAY field.

 
ok thanks for looking, I'll try to get it figured out!

leslie
 
Do yourself a favour and download the JVCL and JCL libraries. The JCL contains many useful functions including:

StrPadLeft and StrPadRight

Which are as follows:

Code:
function StrPadLeft(const S: AnsiString; Len: Integer; C: AnsiChar): AnsiString;
var
  L: Integer;
begin
  L := Length(S);
  if L < Len then
    Result := StringOfChar(C, Len - L) + S
  else
    Result := S;
end;

function StrPadRight(const S: AnsiString; Len: Integer; C: AnsiChar): AnsiString;
var
  L: Integer;
begin
  L := Length(S);
  if L < Len then
    Result := S + StringOfChar(C, Len - L)
  else
    Result := S;
end;

I've done several types of EFT type files and I find it easier on my brain to build each part of the output in stages rather than using the Format function. You can also use the above function to pad your text strings as well. Such as:

Code:
RecordString := '';
//30 characters for address 1
RecordString := RecordString + 
  StrPadRight(trim(FieldByName('STREET1').AsString),30,' '); 

//30 characters for address 2
RecordString := RecordString + 
  StrPadRight(trim(FieldByName('STREET2').AsString),30,' '); 

RecordString := RecordString + '21801';

RecordString := RecordString + '21801';
...
RecortString := RecordString + FormatFloat('00000000000',Trunc(FieldByName('TOTALPAY').AsFloat * 100));

Cheers,

Django
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top