[fpc-pascal] Formatting Question

Vojtěch Čihák vojtech.cihak at atlas.cz
Sat Apr 3 19:02:31 CEST 2021


Hi,
 
I tried this (add StrUtils to uses):
 
procedure TForm1.Button1Click(Sender: TObject);
var i, l: Integer;
    aV: Double;
    aF: TFormatSettings;
begin
  aF.DecimalSeparator:='.';
  aF.ThousandSeparator:=' ';
  for i:=-2 to 10 do
    begin
      aV:=pi*power(10, i);
      l:=length(intToStr(trunc(aV)));
      l:=15-l-((l-1) div 3);
      writeln(AddChar(' ', '', l)+FormatFloat('#,##0.######', aV, aF));
    end;
end;
 
Tweak it to your needs, it aligns well to console (up to 999 999 999 999.xxxxx) here.
 
V.
______________________________________________________________
> Od: "James Richters via fpc-pascal" <fpc-pascal at lists.freepascal.org>
> Komu: "'FPC-Pascal users discussions'" <fpc-pascal at lists.freepascal.org>
> Datum: 03.04.2021 17:53
> Předmět: [fpc-pascal] Formatting Question
>
I'm looking for a way to format numerical data in a string so that everything ends up aligned by the decimal point. 
 I've been trying to use the Format() function but I don't see how to do what I am looking for... 
 then again I don't really understand the format() function, and most of the examples show exponents, which I do not want.
 
 My input variables are all Doubles and I want the result to be padded with spaces before the decimal point if needed and 
 trailing zeros to be replaced with spaces so that it always is the same total width and the decimal point is always in the same position.
 The string will later be either output to the console or saved into a file.  It will only ever be viewed with a fixed width font.
 
 Does anyone have any idea if this can be done with any function included with FPC or am I going to write my own function to do this?
 
 James
 
 
 _______________________________________________
 fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
 https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20210403/0260cecc/attachment.htm>


More information about the fpc-pascal mailing list