Re: [fpc-pascal] Formatting Question
0.5 2.53 12.5 Thanks! That’s exactly that I was looking for. Thank you for sharing your code! James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Formatting Question
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 <mailto:ja...@productionautomation.net> ja...@productionautomation.net (813)-763-1110 From: fpc-pascal On Behalf Of Jean SUZINEAU via fpc-pascal Sent: Saturday, April 3, 2021 4:41 PM To: fpc-pascal@lists.freepascal.org 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Formatting Question
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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Formatting Question
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.x) here. V. __ 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 ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal <https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal> ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Formatting Question
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. On 4/3/2021 11:43 AM, James Richters via fpc-pascal wrote: 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@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[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@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal