Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Comma formatted numbers

37 views
Skip to first unread message

Real Troll

unread,
Jan 23, 2018, 1:21:19 PM1/23/18
to

Few weeks ago somebody asked a question about formatting numbers with
commas or some locale customs. The short answer is it can be done as in
this example:

<+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>

// imbue example
#include <iostream> // std::cout
#include <locale> // std::locale

using namespace std;

int main()
{
locale mylocale(""); // get global locale
cout.imbue(mylocale); // imbue global locale
cout << 1000000 << endl;
return 0;
}

<+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>

This code works in all versions of Visual Studio since 2010 but none of
the Embarcadero products support this!!

I haven't tested this in other compilers yet. The result of the above
program should be:

1,000,000

NOT 1000000.

Paavo Helde

unread,
Jan 23, 2018, 2:31:15 PM1/23/18
to
Depends on the locale. I just tested it and on my machine the output is:

1á000á000

This is not even close to anything expected. I don't even feel like
curious any more where and how it goes wrong. IMO all the locale stuff
is just misfeatures, starting from the hidden global state up to just
not working at all. In my code I have spent lots of efforts to avoid any
locale-dependent behavior. The last occasion was when I discovered that
stricmp() appears in the profiler output, in a program which ought to
just crunch numbers basically.



Rick C. Hodgin

unread,
Jan 23, 2018, 2:34:25 PM1/23/18
to
Can you write your own algorithm and move on? It's a quick task.

--
Thank you, | Indianapolis, Indiana | God is love -- 1 John 4:7-9
Rick C. Hodgin | http://www.libsf.org/ | http://tinyurl.com/yaogvqhj
-------------------------------------------------------------------------
Software: LSA, LSC, Debi, RDC/CAlive, ES/1, ES/2, VJr, VFrP, Logician
Hardware: Arxoda Desktop CPU, Arxita Embedded CPU, Arlina Compute FPGA

Alf P. Steinbach

unread,
Jan 23, 2018, 5:01:16 PM1/23/18
to
On 1/23/2018 7:25 PM, Real Troll wrote:
>
> Few weeks ago somebody asked a question about formatting numbers with
> commas or some locale customs.  The short answer is it can be done as in
> this example:
>
> <+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
>
>
> // imbue example
> #include <iostream>     // std::cout
> #include <locale>       // std::locale
>
> using namespace std;
>
> int main()
> {
>    locale mylocale("");   // get global locale

"" is the user's natural locale.

The global locale at startup is instead the C locale.


>    cout.imbue(mylocale);  // imbue global locale
>    cout << 1000000 << endl;
>    return 0;
> }
>
> <+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>
>
>
> This code works in all versions of Visual Studio since 2010 but none of
> the Embarcadero products support this!!
>
> I haven't tested this in other compilers yet.  The result of the above
> program should be:
>
> 1,000,000
>
> NOT 1000000.

There's no "should" about it, as far as I know.

Here are two results, respectively MinGW g++ and Visual C++:


[H:\forums\clc++\031 locale]
> g++ main.cpp

[H:\forums\clc++\031 locale]
> a
1000000

[H:\forums\clc++\031 locale]
> cl main.cpp /Feb
main.cpp

[H:\forums\clc++\031 locale]
> b
1,000,000

[H:\forums\clc++\031 locale]
> _


Cheers!,

- Alf

Real Troll

unread,
Jan 23, 2018, 5:04:21 PM1/23/18
to
Which compiler did you use? I tested in Visual Studio because it is up
to date and Herb Sutter is Microsoft Evangelist!!! He is: "software
architect at Microsoft where he has led the language extensions design
of C++/CLI, C++/CX, C++ AMP, and other technologies". As you may know
Herb is the boss of ISO C++ Standard!!!!!

Real Troll

unread,
Jan 23, 2018, 9:23:42 PM1/23/18
to
On 23/01/2018 22:01, Alf P. Steinbach wrote:
>
> There's no "should" about it, as far as I know.
>
> Here are two results, respectively MinGW g++ and Visual C++:
>
>
> [H:\forums\clc++\031 locale]
> > g++ main.cpp
>
> [H:\forums\clc++\031 locale]
> > a
> 1000000
>

OK try this one.

<=============================================================================>
#include <iostream> // std::cout
#include <locale> // std::locale

using namespace std;

class MyNumPunct : public numpunct<char>
{
protected:
virtual char do_thousands_sep() const { return ','; }
virtual string do_grouping() const { return "\03"; }
};

int main()
{
cout.imbue( locale( locale::classic(), new MyNumPunct ) );
cout << 123456789 << endl;
return 0;
}

<=============================================================================>


Alf P. Steinbach

unread,
Jan 24, 2018, 12:58:20 AM1/24/18
to
Assuming that the code is technically correct (names, values) that
should work portably.

You just can't rely on defaults.

And note: there's no UTF-8 locale in Windows, and the only two portable
locale names, as I recall, are "" (user's natural locale) and "C".


Cheers!,

- Alf

Paavo Helde

unread,
Jan 24, 2018, 1:56:56 AM1/24/18
to
Microsoft Visual C++ 2017.

The thousands separator is a space in my locale, just checked it in the
Control Panel. Wait, let's recheck ... no, it's not a space, it just
looks like a space, when copy-pasted it appears as char(-96), aka
character code 160. Apparently this has been intended to be the NBSP
character in ISO-Latin-1 or Unicode.

So it appears that although Windows has been internally using Unicode
for at least 20 years they have not yet figured out how to display it in
a console window - and are probably blaming me for, er, something.

BTW, using std::wcout instead of std::cout in the program or renaming
main() to wmain() did not make any difference.

Cheers
Paavo

PS. I'm sure Herb is doing his best but he cannot reinvent the whole
Windows.

0 new messages