Recent

Author Topic: [SOLVED] Format numbers and thousand separation in LazReport  (Read 764 times)

artem101

  • Full Member
  • ***
  • Posts: 114
[SOLVED] Format numbers and thousand separation in LazReport
« on: October 30, 2025, 08:06:18 am »
In my report template I display total sum with thousand separators.

Code: Pascal  [Select][+][-]
  1. Sum: [[field name] #N# ##0] rub.

It correctly shows 1234 as "1 234". But 12345678 looks like "12345 678". I need "12 345 678". How to fix? Thanks.
« Last Edit: October 30, 2025, 01:30:18 pm by artem101 »

artem101

  • Full Member
  • ***
  • Posts: 114
Re: Format numbers and thousand separation in LazReport
« Reply #1 on: October 30, 2025, 01:30:04 pm »
Found solution:
Quote
#N# ### ##0

dseligo

  • Hero Member
  • *****
  • Posts: 1651
Re: [SOLVED] Format numbers and thousand separation in LazReport
« Reply #2 on: October 30, 2025, 02:31:52 pm »
It looks like LazReport calls FormatFloat from FP.
And FormatFloat probably doesn't handle space as thousand separator well.

If there is more digits in the number, your solution stops working.

So for 1234567890 you need to add more hashtags:
Code: Pascal  [Select][+][-]
  1. ShowMessage(FormatFloat('### ### ### ##0', 1234567890));

:)

P.S.: And that solution adds spaces from left side if number is smaller.
« Last Edit: October 30, 2025, 02:35:15 pm by dseligo »

wp

  • Hero Member
  • *****
  • Posts: 13334
Re: [SOLVED] Format numbers and thousand separation in LazReport
« Reply #3 on: October 30, 2025, 06:06:23 pm »
So for 1234567890 you need to add more hashtags:

P.S.: And that solution adds spaces from left side if number is smaller.
FormatFloat is meant to have the ThousandSeparator symbol (',') in the format mask: When this symbol is found the integer digits are grouped by three and separated by the Decimalseparator of the FormatSettings:
Code: Pascal  [Select][+][-]
  1.  FormatFloat('#,##0', 1234567890)  ---> '1,234,567,890'  (when FormatSettings.ThousandSeparator = ',')
  2.  FormatFloat('#,##0', 1234567890)  ---> '1 234 567 890'  (when FormatSettings.ThousandSeparator = ' ')
  3.  FormatFloat('#,##0.0', 1234567890.123)  ---> '1.234.567.890,1'  (when FormatSettings.ThousandSeparator = '.' and .Decimalseparator = ',')
There is no need to have several '#' symbols, in fact FormatFloat(',#', 1234567890) has the same output as the first example above, and FormatFloat(',.0', 1234567890.123) as the last example.

But I am not sure whether this has any relevance to the OP's issue. I do not understand that 'N' inside the format string.
« Last Edit: October 30, 2025, 06:08:59 pm by wp »

artem101

  • Full Member
  • ***
  • Posts: 114
Re: [SOLVED] Format numbers and thousand separation in LazReport
« Reply #4 on: October 30, 2025, 08:32:02 pm »
I do not understand that 'N' inside the format string.
Possible mistake. I copy-pasted this from somewhere.

artem101

  • Full Member
  • ***
  • Posts: 114
Re: [SOLVED] Format numbers and thousand separation in LazReport
« Reply #5 on: October 31, 2025, 04:59:34 pm »

If there is more digits in the number, your solution stops working.


Not a problem. It enough to me.

dseligo

  • Hero Member
  • *****
  • Posts: 1651
Re: [SOLVED] Format numbers and thousand separation in LazReport
« Reply #6 on: November 02, 2025, 03:07:13 am »
There is no need to have several '#' symbols, in fact FormatFloat(',#', 1234567890) has the same output as the first example above, and FormatFloat(',.0', 1234567890.123) as the last example.

But I am not sure whether this has any relevance to the OP's issue. I do not understand that 'N' inside the format string.

I looked more into this problem.
'N' inside the format string is a way for LazReport to format values for each field (there could also be 'D' and 'T' for dates and times, and 'B' for boolean values).
Problem is that LazReport changes format string: it tries to guess decimal separator (in InternalOnGetValue) and temporarily changes default decimal separator (in FormatValue function, before it calls FormatFloat or FloatToStrF). So this doesn't work well in countries where decimal separator is comma.
It would be best for LazReport to leave format string and to just call FormatFloat, but changing that would probably break some existing reports.

 

TinyPortal © 2005-2018