Lazarus

Programming => General => Topic started by: eduardo_result on December 04, 2019, 10:15:10 pm

Title: Problems with formatfloat
Post by: eduardo_result on December 04, 2019, 10:15:10 pm
After updating lazarus to version 2.0.6 I am having problems with formatfloat, before I used version 1.4.x, for sample the number 18670.10 using FormatFloat ('###, ## 0.00', Value) presents 1.8670,10
Title: Re: Problems with formatfloat
Post by: winni on December 04, 2019, 10:36:19 pm
Hi!

I have not understood your Formatstring but that works:

Code: Pascal  [Select][+][-]
  1. showMessage(FormatFloat ('#,##0.00', 18670.10 ));

Winni
Title: Re: Problems with formatfloat
Post by: wp on December 04, 2019, 11:01:25 pm
After updating lazarus to version 2.0.6 I am having problems with formatfloat, before I used version 1.4.x, for sample the number 18670.10 using FormatFloat ('###, ## 0.00', Value) presents 1.8670,10
The comma in the format string means "display thousand separator(s) in the output string". If you don't want that use the format string '0.00', i.e. FormatFloat('0.00', Value)

And are there any spaces in your format string? Remove them.
Title: Re: Problems with formatfloat
Post by: eduardo_result on December 05, 2019, 02:00:45 am
The problem is in the value display.

This code: ShowMessage(FormatFloat('###,##0.00',18670.10));
Show 1.8670,10 when should show 18.670,00
Title: Re: Problems with formatfloat
Post by: kapibara on December 05, 2019, 03:21:25 am
It shows 18,670.10 on my computer (Linux Debian 10, 64bit)

The problem is in the value display.

This code: ShowMessage(FormatFloat('###,##0.00',18670.10));
Show 1.8670,10 when should show 18.670,00
Title: Re: Problems with formatfloat
Post by: wp on December 05, 2019, 12:23:32 pm
I can confirm the issue now. I remember that there was a bug report on it; a test shows that it is fixed with FPC 3.2. The FPC version 3.0.4 used by the Laz 2 series, however, shows the issue; older versions, 2.6.x, are fine again.

Unfortunately FPC 3.2 has not yet been released. In the meantime you can by-pass the issue by using a simpler format string: '#,##0.00' works also with FPC 3.0.x.

Code: Pascal  [Select][+][-]
  1. ShowMessage(FormatFloat('#,##0.00',18670.10));

P.S.
The placeholder '#' is not evaluated in the integer part, only in the fractional part. Therefore, it is sufficient to use ',0.00' - the comma indicates that thousand separators will have to be inserted when needed.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   ShowMessage(FormatFloat(',0.##',18670.11234567890) + LineEnding +
  4.               FormatFloat(',0.##',189701232123.1212121) + Lineending +
  5.               FormatFloat(',0.##',18970)
  6.   );
  7. end;  
Title: Re: Problems with formatfloat
Post by: Zoran on December 05, 2019, 02:57:17 pm
You can get the same with FloatToStrF (https://www.freepascal.org/docs-html/current/rtl/sysutils/floattostrf.html) or Format (https://www.freepascal.org/docs-html/current/rtl/sysutils/format.html) instead of FormatFloat:
FloatToStrF(18670.10, ffNumber, 18, 2);
or
Format('%.2N', [18670.10]);
Title: Re: Problems with formatfloat
Post by: eduardo_result on December 06, 2019, 02:41:42 pm
in this format shows correct: FormatFloat(',0.00',18670.10);

Thank you
TinyPortal © 2005-2018