Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable commas in cout?

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.

like image 537
dinkelk Avatar asked Jun 19 '13 22:06

dinkelk


2 Answers

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

like image 143
Jerry Coffin Avatar answered Sep 20 '22 11:09

Jerry Coffin


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>()));
like image 40
Jesse Good Avatar answered Sep 18 '22 11:09

Jesse Good