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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format String issues 1

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
So I am trying to use the following:
Code:
Format('%-55s%-25m%-25m%-25m', [FieldByname('Fullname').AsString, FieldByName('HourPay').AsFloat, FieldByName('MilePay').AsFloat, FieldByName('TotalPay').AsFloat]));

but it's not working like I would expect.

I want my results to look like:
[tt]
Joe Blow $100.25 $36.23 $136.48
Jane Doe $81.50 $6.40 $87.90
Leslie Andrews $105.36 $0.00 $105.36[/tt]

I have tried taking out the -; that didn't work. I've tried adding a .; that didn't work either!

What do I need to have in the format fuction to get it to line up???

Thanks!
leslie
 
So you're wanting the dollar values to be right justified instead of left justified?

If the string formatting you are using works anything like the standard writeln formatting (if it works like C string formatting, it does), you'll need to insert a number of spaces into the front of the value, so the string formatting will line up.

For example:
$100.25
$81.50
$105.36

The second value has a smaller string length, so it needs one space in front of it before it will right-justify.

Perhaps best is to write a generic function that will agree with the rest of your string formatting. Something that always returns a set length of string, and anything smaller that goes into it gets spaces added to it until it returns that length of string.

----------
Measurement is not management.
 
At this point I don't care if it's left or right! I just want it lined up! I have a query, I pass the result set to this function that prints each row in the query.
What I get now from the above Format function is:
[tt]
CHRISTINE JENS $120.25 $0.00 $120.25
LORI GALLEGO $214.50 $0.00 $214.50
CHRISTINA QUILLMAN $208.00 $26.88 $234.88
THERESA PRICE $87.75 $0.00 $87.75
TRUDI GONZALEZ-HOUSED $195.00 $0.00 $195.00[/tt]


Leslie

Have you met Hardy Heron?
 
Okay, I understand a bit more now...I forgot about the names...you want to justify your next row off of them, too. But again differing lengths. So you'll need to add spaces to those, too (on the end), so you'll get uniform spacing for the report.

(BTW, this stuff is common for reports - you can't just go with a single format statement, generally, and get it lined up)

Code:
function space_add_r(instr: string; sl: integer): string;
  var
    outstr: string;
    i: integer;
  begin
    outstr := instr;
    if Length(Instr) < sl then
      for i := Length(Instr) to sl do
         outstr := outstr + ' ';
    Result := outstr;
  end;

The function to add spaces to the left side of the dollar amounts is similar.

----------
Measurement is not management.
 
Or you could probably dynamically determine the format string, based on the lengths of the values and what you need.

----------
Measurement is not management.
 
except I use the Format function elsewhere and it creates a fixed width file format. That's basically what I'm trying to do is create a fixed width output.

I'm writing this directly to the printer using TPrinter commands.
 
for the printer,
are you using a fixed character width font?

examples are : courier new, FixedSys, ...

Arial and others have variable width. (compare it with the tt TGML option)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
except I use the Format function elsewhere and it creates a fixed width file format. That's basically what I'm trying to do is create a fixed width output.

And you are creating a fixed width output here as well. You have a name, then 8 spaces, then one dollar figure and 8 spaces then another dollar figure and 10 spaces, and then the last dollar amount. You are consistently doing that with each report row.

The problem is that for your human-readable report (much different than a file) you don't want a fixed width output. You want each column to begin, or end (depending on the data type), on a specific spot on the page. To do that requires varying numbers of spaces for each line.

That's what I'm telling you.

----------
Measurement is not management.
 
changing the font was the easiest suggestion to implement!

It's just the way I want it now!

thanks Glenn & /Daddy for helping get this worked out!

Leslie
 
Well you guys beat me with the fixed font suggestion. But for your data size this produces the best format appearance- wise:
Code:
memo1.Lines.Add(Format('%-20s %8.2m %8.2m %8.2m', [aName, val1, val2, val3]));

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top