The localeconv() function just read the localization settings, and ptrLocale->thousands_sep itself will not change the settings for the current locale.
EDIT:
I do not know how to do this in C, but you can find many examples with C ++ output. 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:
C ++ link:
localeconv () returns a pointer to a populated 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 ββ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 this is a bad and insecure 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 changes to decimal_point and thousands_sep possible using the pointer obtained 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* }
source share