Set thousands separator for C printf
Function localeconv()
just read locate settings, and ptrLocale->thousands_sep
itself not changes that settings for current locale.
EDIT:
I do not know how to do this in C, but lots of examples with C++ output can be found. See the following example in C++:
#include <iostream>
#include <locale>
using namespace std;
struct myseps : numpunct<char> {
// use ' as separator
char do_thousands_sep() const { return '\''; }
// digits are grouped by 3
string do_grouping() const { return "\3"; }
};
int main() {
cout.imbue(locale(locale(), new myseps));
cout << 1234567; // the result will be 1'234'567
}
EDIT 2:
The C++ reference said:
localeconv() returns a pointer to a filled-in object of type struct lconv. The values contained in the object can be overwritten by subsequent calls to localeconv and do not directly modify the object. Calls to setlocale with category values of LC_ALL, LC_MONETARY, or LC_NUMERIC overwrite the contents of the structure.
I tried the following example in MS Visual Studio 2012 (I understand that it is bad and unsafe style):
#include <stdio.h>
#include <locale.h>
#include <string.h>
int main() {
setlocale(LC_NUMERIC, "");
struct lconv *ptrLocale = localeconv();
strcpy(ptrLocale->decimal_point, ":");
strcpy(ptrLocale->thousands_sep, "'");
char str[20];
printf("%10.3lf \n", 13000.26);
return 0;
}
and I saw the result:
13000:260
therefore, it can be assumed that the changes of decimal_point
and thousands_sep
are possible through pointer received with localeconv()
, but printf
ignores thousands_sep
.
EDIT 3:
Updated C++ example:
#include <iostream>
#include <locale>
#include <sstream>
using namespace std;
struct myseps : numpunct<char> {
// use ' as separator
char do_thousands_sep() const { return '\''; }
// digits are grouped by 3
string do_grouping() const { return "\3"; }
};
int main() {
stringstream ss;
ss.imbue(locale(locale(), new myseps));
ss << 1234567; // printing to string stream with formating
printf("%s\n", ss.str().c_str()); // just output when ss.str() provide string, and c_str() converts it to char*
}
Here is a very simple solution which works on each linux distribution and does not need - as my 1st answer - a glibc
hack:
All these steps must be performed in the origin glibc
directory - NOT in the build directory - after you built the glibc
version using a separate build directory as suggested by this instructions.
My new locale
file is called en_AT
.
- Create in the
localedata/locales/
directory from an existing fileen_US
a new fileen_AT
. - Change all entries for
thousands_sep
tothousands_sep "<U0027>"
or whatever character you want to have as the thousands separator. - Change inside of the new file all occurrences of
en_US
toen_AT
. - Add to the file
localedata/SUPPORTED
the line:en_AT.UTF-8/UTF-8 \
. - Run in the build directory
make localedata/install-locales
. - The new
locale
will be then automatically added to the system and is instantly accessible for the program.
In the C/C++ program you switch to the new thousands separator character with:
setlocale( LC_ALL, "en_AT.UTF-8" );
using it with printf( "%'d", 1000000 );
which produces this output
1'000'000
Remark: When you need in the program different localizations which are determinated while the runtime you can use this example from the man
pages where you load the requested locale
and just replace the LC_NUMERIC
settings from en_AT
.