bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf issue with thousands separator


From: Christian Robert
Subject: Re: printf issue with thousands separator
Date: Tue, 5 Jul 2022 19:33:17 -0400
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0

Jürgen,

#include <stdio.h>
#include <locale.h>

int
main(int argc, char *argv[])
{
   setlocale (LC_NUMERIC, "en_US.utf8");

   printf("%'f\n", 555555.66);
}

print:

555,555.660000


my 2 cents...

Xtian.


On 2022-07-05 12:40, Dr. Jürgen Sauermann wrote:
Hi Martin,

I believe I understood your problem now. As far as *⎕FIO* is concerned it 
should be fixed
in *SVN 1579*:

*      "%'.2f" ⎕FIO.sprintf 1234567.89**
**1,234,567.89**
*

There could be a misunderstanding as to how 'Format by example' works (and that 
misunderstanding
might also be caused by my testcases). What the APL2 calls a left or right 
"decorator" is not intended
as an arbitrary string in the printf() fashion, but something that is directly 
attached (and possibly
suppressed) to a number. In most cases the left decorator is either the minus 
sign or the overbar and
the right decorator is not used. The rare exceptions where the right decorator 
is used are:

1. negative numbers printed in parentheses instead of a minus sign. Sometimes 
used in bookkeeping.
2. output of negative numbers in a different color (typically red) using VT100 
escape sequences as
left and right decorators.

In the examples that you apparently tried there were 2 left decorators (one 
being the $
and the other being the minus sign) and Format by example simply does not allow 
that.

The problem with ⎕FIO was actually caused by glibc which apparently ignores  
the ' flag even
if the locale tells otherwise, On my machine (and in plain C):

*int
main(int argc, char *argv[])
{
    printf("%'f\n", 555555.66);
}
*
prints:

*555555.660000*

I did already pass the ' flag to *sprintf() *in glibc but glibc then simply 
ignored it.

Best Regards,
Jürgen



On 7/5/22 10:08 AM, Martin Michel wrote:
Hi Jürgen,

thanks for your quick reply.

I will look into adding support for the thousands separator (but not supporting 
the
locales nonsense). That is, the thousands separator will always be comma and not
e.g. full-stop like in some countries and comma in others.
That would be great and I fully agree, locales support would be too much
of a requirement.

BTW format by example should have done the job (see the APL2 language
reference page 140):


       "5,555.50" ⍕ 1234.56
1,234.56
Well, I tried this but it only works for the simpler cases. I could
manage to format figures with the currency symbol on the right side but
the format by examples falls short if I want it on the left, combined
with negative numbers and also rounding (e.g. no decimal point in
example spec).

So this works:

       ' -1,555,555.40 $' ⍕ 123456.789 ¯987654.12
      123,456.79 $   -987,654.12 $

But I have not found anything which would give me these results:

     $ +123,457   $ -987,654

As far as I have understood IBM APL2 Language Reference this is not
possible with format by example. I would be happy if you can convince me
otherwise, then I would indeed not need ⎕FIO.

Kind regards,
Martin









reply via email to

[Prev in Thread] Current Thread [Next in Thread]