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*
}