So typically if I wanted to insert locale appropriate separators in some number, foo
, I'd do something like this:
ostringstream out;
out.imbue(locale("en-US"));
out << foo;
Then I could just use out.str()
as the separated string: http://coliru.stacked-crooked.com/a/054e927de25b5ad0
Unfortunately I've been asked not to use stringstreams
in my current project. Is there any other way I can accomplish this? Ideally a locale dependent way?
So this answer is the C++ distillation of Jerry Coffin's answer to this question: Cross Platform Support for sprintf's Format '-Flag
Naturally this suffers from the identical 2's compliment negation issue.
This certainly benefits from C++'s
string
memory management, but also from the ability to pass in a specificnumpunct<char>
which need not be the current locale. For example whether or notcout.getloc() == locale("en-US")
you can call:commafmt(foo, use_facet<numpunct<char>>(locale("en-US")))
Live Example
Set up a pipe. Use the linked to code to construct an ofstream and ifstream from a file descriptor and then output to one and read from the other.
This is a bizarre and contorted solution. But then the idea that you have to use locales, have to store things, and can't use
stringstream
is just as bizarre. So, they give you bizarre requirements, they get bizarre code.