Disable commas in cout?
Asked Answered
F

2

8

In a project I am currently working on I link to a proprietary dynamic library. As soon as I run the library's initialize function, the behavior of logging and printing of numbers changes.

Commas have been inserted at every third decimal. Ie.

cout << 123456789 << endl

used to print out 123456789 and now it prints 123,456,789. This is horribly annoying, because this behavior is not what I want.

After some research I suspect a locale issue. I have tried using this line of code after calling the initialize function

setlocale(LC_ALL,"C");

thinking it might reset my local to the default; but to no avail. The commas persist!!

What am I missing?

I have posted a related follow on question here.

Fpc answered 19/6, 2013 at 22:15 Comment(0)
S
6

You can set the locale for a stream, independent of the locale that's set with setlocale. Try std::cout.imbue(std::locale("C"));

Straightaway answered 19/6, 2013 at 22:18 Comment(1)
that worked great for local variables in my main! However, the executable I am running is also linked to 10+ custom shared libraries. The comma error has unfortunately propagated into those shared libraries stringstreams and couts as well. Is there a better way to globally set the local, instead of going through all the source code for my libraries and adding in imbue(std::locale("C")) for every stringstream I find?Fpc
A
2

If you just want to get rid of the commas, you could also replace the current std::numpunct which is probably causing it with the default one which does not override do_grouping.

std::cout.imbue(std::locale(std::cout.getloc(), new std::numpunct<char>()));
Apterygial answered 19/6, 2013 at 22:29 Comment(7)
@ Jesse Good, thanks for the suggestion. This works well for some of my cases. See my edit above.Fpc
@Fpc You should only ask one question per post. I suggest you create a new question addressing the issue. :)Whack
@0x499602D2 The answers here have not fully fixed my problem. The question has not changed, just the specifics.Fpc
@dinkelk: Are you sure those libraries do not provide a way to configure the settings? If those stringstreams imbue there own locale, there isn't a way to modify that globally.Apterygial
@Jesse Good I will take a look. I assumed that because the library itself was able to modify my stringstream's locale globally, I could also reset their locale globally.Fpc
@Fpc As I see it, Stack Overflow isn't here to actually solve your problem, it's to come up with good answers to specific questions, which will be of general use to others in the future. If you have a further question which needs answering to solve your problem, ask a separate question.Sphery
@Peter Wood, I agree with that. I have posted a separate question and removed my edit.Fpc

© 2022 - 2024 — McMap. All rights reserved.