Forum > FPC development
floattostr with fixed width
DesJardins:
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:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var Str:String;begin WriteStr(Str,pi:22:3); writeln(Str);end;
DesJardins:
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:
--- Quote from: DesJardins on December 14, 2017, 01:34:01 am ---That really doesn't solve the problem.
--- End quote ---
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.
--- Quote from: DesJardins on December 14, 2017, 01:34:01 am ---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.
--- End quote ---
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.
--- Quote from: DesJardins on December 14, 2017, 01:34:01 am ---How do you do that?
--- End quote ---
Personally I use a set of padStr functions I wrote long ago for delphi. Something along the lines of
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Function LeftPadStr(const aValue:String; aLength:Integer; aChar:Char = ' '):String;var vLen : integer;begin vLen := aLength - Length(aValue); if vLen > 0 then begin setlength(Result,vLen); Fillchar(Result[1],vLen, aChar); result := Result + aValue; end else begin Result := aValue; setLength(Result,aLength); end;end; 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.
ccrause:
--- Quote from: DesJardins on December 06, 2017, 11:50:45 pm ---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.
--- End quote ---
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.
Navigation
[0] Message Index
[#] Next page