Recent

Author Topic: floattostr with fixed width  (Read 4858 times)

DesJardins

  • New Member
  • *
  • Posts: 26
floattostr with fixed width
« on: December 06, 2017, 11:50:45 pm »
in Python I can print using "float to string" with not only a fixed number of decimal places, but also with a fixed width of the print cell so columns will line up when I print several lines of:  name 1st column), variable 1 (2nd column), variable 2 (3rd column).

Is there a way to do this in FPC?

when I save to a file ("f") I can print it with Notes and it prints in good columns:  EXAMPLE:

Writeln(f,'                  *** RESULTS OF COMBINING TWO AIR STREAMS ***');
writeln(f);
WRITELN(f,'                                           STREAM #1   STREAM #2   COMBINED');
write(f,'  DRY BULB TEMP (DEGREES F)            ' ,TEMPDB1:12:3);
write(f,TEMPDB2:12:3);
writeln(f,TempDB3:12:3);
write(f,'  WET BULB TEMP (DEGREES F)            ' ,TEMPWB1:12:3);
write(f,TEMPWB2:12:3);
writeln(f,TempWB3:12:3);

But when I send it to printer unit ("PU") for direct printing the columns don't line up  EXAMPLE:

PU('                *** RESULTS OF COMBINING TWO AIR STREAMS  METRIC UNITS---***');
PU('');
PU('                                                                        STREAM #1   STREAM #2     COMBINED');
PU(' DRY BULB TEMP (DEGREES C)                       ' +floattostrf(tempdb1si,fffixed,22,3)+('             ')+floattostrf(tempdb2si,fffixed,22,3)+('            ')+floattostrf(tempdb3si,fffixed,12,3));
PU(' WET BULB TEMP (DEGREES C)                      ' +floattostrf(tempwb1si,fffixed,12,3)+('             ')+floattostrf(tempwb2si,fffixed,12,3)+('            ')+floattostrf(tempwb3si,fffixed,12,3));

And it is worse and different for different printers.  What I show here is almost okay for a Brother pinter, but not for a Canon printer.  So, how do I fix the width of the floattostr without going to Grid or Canvas which makes everything much more complicated.  Seems like FPC needs to be able to do this like other languages do.

The program is simple FPC, not Lazarus.

Richard

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: floattostr with fixed width
« Reply #1 on: December 07, 2017, 12:02:26 am »
Code: Pascal  [Select][+][-]
  1. var
  2.   Str:String;
  3. begin
  4.   WriteStr(Str,pi:22:3);
  5.   writeln(Str);
  6. end;
  7.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

DesJardins

  • New Member
  • *
  • Posts: 26
Re: floattostr with fixed width
« Reply #2 on: December 14, 2017, 01:34:01 am »
That really doesn't solve the problem.  I am not worried about the length of the number or the number of digits after the decimal, what I want it to do is fit a column that is, say,  12 or 22 or 33  column spaces wide either right or left aligned in the column.  How do you do that?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: floattostr with fixed width
« Reply #3 on: December 14, 2017, 02:25:15 am »
That really doesn't solve the problem.
OK. I have no idea why though it has all the writeln abilities and it formats everything to a string that you can use anywhere you like.
I am not worried about the length of the number or the number of digits after the decimal, what I want it to do is fit a column that is, say,  12 or 22 or 33  column spaces wide either right or left aligned in the column.
Well the code I posted, at least in my installation, will right align the string with the proper number of spaces in front to fill the 22 spaces you defined.
How do you do that?
Personally I use a set of padStr functions I wrote long ago for delphi. Something along the lines of
Code: Pascal  [Select][+][-]
  1. Function LeftPadStr(const aValue:String; aLength:Integer; aChar:Char = ' '):String;
  2. var
  3.   vLen : integer;
  4. begin
  5.   vLen := aLength - Length(aValue);
  6.   if vLen > 0 then begin
  7.     setlength(Result,vLen);
  8.     Fillchar(Result[1],vLen, aChar);
  9.     result := Result + aValue;
  10.   end else begin
  11.     Result := aValue;
  12.     setLength(Result,aLength);
  13.   end;
  14. end;
  15.  
similarly for RightPadStr;

disclaimer: All code was written directly in the browser and it might contain bugs or other creatures. Make sure you clean it up for your own use.
« Last Edit: December 14, 2017, 02:57:05 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: floattostr with fixed width
« Reply #4 on: December 14, 2017, 06:06:38 am »
And it is worse and different for different printers.  What I show here is almost okay for a Brother pinter, but not for a Canon printer.  So, how do I fix the width of the floattostr without going to Grid or Canvas which makes everything much more complicated.  Seems like FPC needs to be able to do this like other languages do.
It sounds like the problem is that the default font chosen the printers you have tested with your Pascal code is proportional, not fixed width (monospaced).  I'm not familiar with the PU printer unit, check if it has a config method where the font type can be specified to e.g. Courier etc.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: floattostr with fixed width
« Reply #5 on: December 14, 2017, 11:51:37 am »
See http://forum.lazarus.freepascal.org/index.php/topic,35448.msg234280.html#msg234280
Any printer that supports it will select a fixed font. Default search is for LPT1, but USB printers are usually on a higher port like 12 or 13. In that case, use InitPrinter.
Include the printer (without S) unit in your application, not printers.

Specialize a type, not a var.

 

TinyPortal © 2005-2018