Recent

Author Topic: Problems with formatfloat  (Read 2093 times)

eduardo_result

  • Newbie
  • Posts: 3
Problems with formatfloat
« 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

winni

  • Hero Member
  • *****
  • Posts: 1909
Re: Problems with formatfloat
« Reply #1 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

wp

  • Hero Member
  • *****
  • Posts: 7642
Re: Problems with formatfloat
« Reply #2 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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

eduardo_result

  • Newbie
  • Posts: 3
Re: Problems with formatfloat
« Reply #3 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

kapibara

  • Hero Member
  • *****
  • Posts: 532
Re: Problems with formatfloat
« Reply #4 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
Lazarus trunk / fpc 3.0.4 / Debian 10 - 64 bit

wp

  • Hero Member
  • *****
  • Posts: 7642
Re: Problems with formatfloat
« Reply #5 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;  
« Last Edit: December 05, 2019, 03:28:22 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Zoran

  • Hero Member
  • *****
  • Posts: 1592
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Problems with formatfloat
« Reply #6 on: December 05, 2019, 02:57:17 pm »
You can get the same with FloatToStrF or Format instead of FormatFloat:
FloatToStrF(18670.10, ffNumber, 18, 2);
or
Format('%.2N', [18670.10]);

eduardo_result

  • Newbie
  • Posts: 3
Re: Problems with formatfloat
« Reply #7 on: December 06, 2019, 02:41:42 pm »
in this format shows correct: FormatFloat(',0.00',18670.10);

Thank you

 

TinyPortal © 2005-2018