Discussion:
[fpc-pascal] Formatting Question
James Richters via fpc-pascal
2021-04-03 15:43:52 UTC
Permalink
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-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listin
Travis Siegel via fpc-pascal
2021-04-03 16:27:51 UTC
Permalink
You can always convert the number to a string, then format it
accordingly.  It's probably not the solution you want, but it will do
the trick.
Post by James Richters via fpc-pascal
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
_______________________________________________
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listin
Vojtěch Čihák via fpc-pascal
2021-04-03 17:02:31 UTC
Permalink
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.
______________________________________________________________
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-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>
Jean SUZINEAU via fpc-pascal
2021-04-03 20:41:22 UTC
Permalink
Normally something like this should do the trick (here 3 decimals and 7
characters for total width):

program Format_Example;
uses
    sysutils;
procedure F( _d: double);
var
   S: String;
begin
     S:= Format('%7.3f',[_d]);
     WriteLn( S);
end;

begin
     F(0.5);
     F(2.53);
     F(12.5);
end.

---------- Output ------------

  0.500
  2.530
12.500
Jean SUZINEAU via fpc-pascal
2021-04-04 07:02:38 UTC
Permalink
That looks almost perfect.. can I suppress the trailing zeros with the
format command without losing the alignment?
As far as I know, no ...

Not  with just the RTL.
Anyway I have personal code for this, you can extract and customize it
to yours needs, it's released under LGPL :

program Format_Example;
uses
    sysutils,uReal_Formatter,uuStrings;
procedure FF( _d: double);
var
   S: String;
begin
     S:= Fixe_MinE( Format_Float(_d, True, 3), 7);
     WriteLn( S);
end;

begin
     FF(0.5);
     FF(2.53);
     FF(12.5);
end.

------- Output -----

  0.5
  2.53
 12.5

You can find  the used units there:

https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/uReal_Formatter.pas

https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/uuStrings.pas
(just extract functions  Fixe_MinE / Fixe_Min0 or you will have to use a
bunch of other units from the same directory
https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/)

https://github.com/jsuzineau/pascal_o_r_mapping/blob/TjsDataContexte/pascal_o_r_mapping/02_Units/u_sys_.pas
James Richters via fpc-pascal
2021-04-04 14:11:21 UTC
Permalink
0.5
2.53
12.5

Thanks! That’s exactly that I was looking for. Thank you for sharing your code!

James

Continue reading on narkive:
Loading...