Lazarus

Programming => General => Topic started by: bmy92 on March 07, 2013, 10:48:25 am

Title: Format with thousands separator
Post by: bmy92 on March 07, 2013, 10:48:25 am
Hi all,

I am a new user of Lazarus 1.06 (FPC 2.6.0), coming from Delphi.
My OS : Windows XP, french.

This instruction :
Code: [Select]
ShowMessage( Format('%10.0n', [1234567.0]) )displays: 1?234?567
Apparently, FPC produces an incorrect thousands separator (a space in my french locale)
Delphi 7 on the same computer correctly gives :  1 234 567

Did I forget something ?
Title: Re: Format with thousands separator
Post by: bigeno on March 07, 2013, 11:03:22 am
You should set default.
DefaultFormatSettings.ThousandSeparator:=' ';
Title: Re: Format with thousands separator
Post by: BigChimp on March 07, 2013, 11:19:50 am
Strange.

Your sample code worked for me (it gave me a period which is correct for my locale) with laz 1.0.6+FPC 2.6.0 as well as Laz trunk +FPC trunk on Vista x64.

Perhaps it's a bug with xp + FPC!?
Title: Re: Format with thousands separator
Post by: bmy92 on March 07, 2013, 12:15:36 pm
I see the same error with my new laptop with Windows 8, french.

Format does take into account the locale for the decimal separator (comma for France), but seems to bug with the thousands separator.
Title: Re: Format with thousands separator
Post by: Blaazen on March 07, 2013, 01:43:19 pm
I add unit "clocale" to uses section of *.lpr file and I can reproduce on Linux. This should be reported.
Title: Re: Format with thousands separator
Post by: bmy92 on March 07, 2013, 03:13:14 pm
The variable ThousandSeparator of sysutils is also incorrectly initialized.

I have created bug report 0024011 http://bugs.freepascal.org/view.php?id=24011
Title: Re: Format with thousands separator
Post by: bmy92 on March 14, 2013, 02:43:26 pm
The bug report 0024011 was finally resolved as "No change required".
This is what I have understood (after hard thinking and many tests).

The non-breaking space provided with the Format function is a character of ordinal value 160 (hexadecimal A0).
This is non-breaking space in Ansi, but we need it in UTF8 encoding, that is hexadecimal C0 A0, or decimal: 192 160

The original problem seems solved with
Code: [Select]
ShowMessage(AnsiToUTF8(Format('%10.0n', [1234567.0])));
But the solution does not work if you have national characters and non-breaking spaces. This will display badly:
Code: [Select]
showMessage(AnsiToUTF8(Format('%10.0n mètres', [1234567.0])));A correct solution is to only convert the Ansi non-breaking space
Code: [Select]
showMessage(AnsiReplaceStr(Format('%10.0n mètres', [1234567.0]), #160, #192#160));

Title: Re: Format with thousands separator
Post by: Blaazen on March 14, 2013, 04:20:01 pm
It does not work for me in console too. When I try simple program:
Code: [Select]
uses clocale, sysutils;

begin
  writeln(FormatSettings.ThousandSeparator);
  writeln(Format('%10.3n', [1234567.0]));
end.
and in console:
Code: [Select]
fpc format.pas
./format

1�234�567,000
Note that I have set no group separator for numbers and '.' for money in KDE4.
Title: Re: Format with thousands separator
Post by: tintinux on September 14, 2015, 01:48:09 pm
Hi,

But don't you think that the best and most natural solution is :


I think that other solutions have side effects and may lead to issues in some specific cases and environments.

And this way LazReport and other libraries will work with this separator and without any change.

Regards
Title: Re: Format with thousands separator
Post by: marcov on September 14, 2015, 01:50:23 pm
(afaik this is already fixed in newer releases)
Title: Re: Format with thousands separator
Post by: tintinux on September 14, 2015, 01:56:05 pm
Good new, thanks !

However it is not fixed in 1.4.2
 
If someone knows in which version, I'm interested. Is it in FPC or Lazarus ?
Title: Re: Format with thousands separator
Post by: marcov on September 14, 2015, 02:26:38 pm
It is not resolved it seems.  FPC is in ansi, LCL requires utf8, and that bites.

Post http://forum.lazarus.freepascal.org/index.php/topic,20163.msg116623.html#msg116623 says it all.

I remember there being a bugreport but erroneously assumed it was fixed. It wasn't it was not fixable.
Title: Re: Format with thousands separator
Post by: otoien on September 15, 2015, 05:08:57 am
BTW, I have never got the console output to work with UTF-8 (opposed to the same write command to a text file). This applies even when using the fixes branch with fpc 3.01 and UTF-8 active in RTL (no FPC-LCL inconsistency), so it seems that console output is not a good test case?
Title: Re: Format with thousands separator
Post by: tintinux on September 16, 2015, 01:26:51 pm
Hi

I have created a bug (http://bugs.freepascal.org/view.php?id=28686) in FPC, to make possible to assign a multibyte character to separators.

Regards
Title: Re: Format with thousands separator
Post by: otoien on September 17, 2015, 02:29:38 am
It is not resolved it seems.  FPC is in ansi, LCL requires utf8, and that bites.

Post http://forum.lazarus.freepascal.org/index.php/topic,20163.msg116623.html#msg116623 says it all.

I remember there being a bugreport but erroneously assumed it was fixed. It wasn't it was not fixable.

Even with both FPC and LCL set to UTF-8 with FPC 3.01 this is not solved according to my testing.

It could be solved by redefining the FormatSettings.ThousandSeparator to a string type. Then it could be assigned the proper UTF-8 code for Thin Space (U+2009) when needed. I do not know how much code would break because of this though.

The operative system's settings must also be able to deal with this - I wonder how it is defined there.

It seems that thin space as a grouping separator is gaining international support,
from  https://en.wikipedia.org/wiki/Decimal_mark (https://en.wikipedia.org/wiki/Decimal_mark) :
" ...the International Bureau of Weights and Measures and International Union of Pure and Applied Chemistry advocate the use of a "thin space" in "groups of three".[15][16] Within the United States, the American Medical Association's widely followed AMA Manual of Style also calls for a thin space.[13]"
Title: Re: Format with thousands separator
Post by: tintinux on September 17, 2015, 10:10:27 am
Hi otoien

Since you have a FPC 3.0, can you read and try the second solution proposed by Bart Broesma (use the "Set Utf-8 in RTL" option in Project Options) in my bug report ?  I do not  understand why, but may be he will explain...

By the way, I don't think a string is needed in ThousandSeparator, a double-byte char would do the job (with a "thin space" too).

Regards

Title: Re: Format with thousands separator
Post by: otoien on September 18, 2015, 02:40:36 am
Of course I have "Set Utf-8 in RTL" active through the compiler directive, that is the reason why I use the fixes branch with fpc 3.01 right now. FormatSettings.ThousandSeparator type is char that only takes one byte, so it is impossible to set it to a multi-byte value required for the UTF-8 version of thin space. I am not aware of any other type than string that can hold a multibyte-byte UTF-8 code point?  [There is widechar, but I do not think it is intended for UTF-8, as it is fixed at two bytes; UTF8 may require more than 2 bytes].
Title: Re: Format with thousands separator
Post by: tintinux on September 18, 2015, 08:53:57 am
Hi

http://wiki.lazarus.freepascal.org/FPC_Unicode_support#PWideChar.2FPUnicodeChar_and_WideChar.2FUnicodeChar (http://wiki.lazarus.freepascal.org/FPC_Unicode_support#PWideChar.2FPUnicodeChar_and_WideChar.2FUnicodeChar)

We need the {$mode delphiunicode} directive, to put UTF8 or other multibyte characters in a type Char, and it is not required AFAIK for strings.

Maybe the directive could solve the issue ?  I can't try before a few days, let us know if you did.

Yes, your suggestion to declare Thousand separator use a string may work too, without requesting the directive, but I don't know if this will be changed in FPC.

Regards

TinyPortal © 2005-2018