Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2021-04

Formatting Question

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

James Richters

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

Travis Siegel
to James Richters

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.

James Richters wrote 16 lines
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 - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Vojtěch Čihák
to James Richters: I'm looking for a way to format numerical data in a string so that everything ends up…
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.
______________________________________________________________
Show quoted text 5 lines
Od: "James Richters via fpc-pascal" Komu: "'FPC-Pascal users discussions'" 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

Jean SUZINEAU
to James Richters: I'm looking for a way to format numerical data in a string so that everything ends up…

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

James Richters
to Jean SUZINEAU

That looks almost perfect.. can I suppress the trailing zeros with the format command without losing the alignment?

James Richters

<http://www.productionautomation.net> www.productionautomation.net

(813)-763-1110

Earlier message 27 lines
From: fpc-pascal On Behalf Of Jean SUZINEAU via fpc-pascal Sent: Saturday, April 3, 2021 4:41 PM To: Cc: Jean SUZINEAU Subject: Re: [fpc-pascal] Formatting Question 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
to James Richters

Le 04/04/2021 à 02:41, James Richters a écrit :

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