another locale Q: lose thousands sep

N

Noah Roberts

A lot of my code converts numeric values to strings using
boost::lexical_cast, and thus, underneath, with <<. When I activate
locales, the thousands separator turns on.

We've deemed it not worthwhile to enable the various input elements to
use thousands separators and so I need to make sure those elements are
never initially filled with them, otherwise the user will never be able
to edit them.

How can I shut this off? I am using my own double type (wrapped up to
do funky rounding) and have some control over the stream function, but
I'd rather finding some format specifier or something to shut this
behavior off entirely. The reason being is that I'm also using
boost::format in this object's << function and it creates its own
streams that I can't override.

So what would be a good way to go about using locales, including numeric
formatting, but without thousands separators anywhere?
 
J

Jerry Coffin

A lot of my code converts numeric values to strings using
boost::lexical_cast, and thus, underneath, with <<. When I activate
locales, the thousands separator turns on.

We've deemed it not worthwhile to enable the various input elements to
use thousands separators and so I need to make sure those elements are
never initially filled with them, otherwise the user will never be able
to edit them.

How can I shut this off? I am using my own double type (wrapped up to
do funky rounding) and have some control over the stream function, but
I'd rather finding some format specifier or something to shut this
behavior off entirely. The reason being is that I'm also using
boost::format in this object's << function and it creates its own
streams that I can't override.

So what would be a good way to go about using locales, including numeric
formatting, but without thousands separators anywhere?

Something like this:

#include <locale>
#include <iostream>

template <class T>
struct formatter : std::numpunct<T> {
protected:
std::basic_string<T> do_grouping() const { return ""; }
};

Create your locale like this:

std::locale no_sep(your_locale, new formatter<char>);

and use this locale in place of your_locale, where you want it to act
the same otherwise, but not allow/produce thousands separators.
 
J

James Kanze

A lot of my code converts numeric values to strings using
boost::lexical_cast, and thus, underneath, with <<. When I activate
locales, the thousands separator turns on.
We've deemed it not worthwhile to enable the various input elements to
use thousands separators and so I need to make sure those elements are
never initially filled with them, otherwise the user will never be able
to edit them.
How can I shut this off? I am using my own double type (wrapped up to
do funky rounding) and have some control over the stream function, but
I'd rather finding some format specifier or something to shut this
behavior off entirely. The reason being is that I'm also using
boost::format in this object's << function and it creates its own
streams that I can't override.

The obvious answer is not to use boost::lexical_cast. If you're
just converting to a string, an asString() template function is
easy to write, and will do exactly what you want. All you need
to do is ensure that the "C" locale is imbued before outputting
to the ostringstream. (It also has the advantage of being
somewhat clearer to the reader. Converting to a string is NOT a
type conversion, aka a cast, in the classical sense.)
 
N

Noah Roberts

Jerry said:
Something like this:

#include <locale>
#include <iostream>

template <class T>
struct formatter : std::numpunct<T> {
protected:
std::basic_string<T> do_grouping() const { return ""; }
};

Create your locale like this:

std::locale no_sep(your_locale, new formatter<char>);

and use this locale in place of your_locale, where you want it to act
the same otherwise, but not allow/produce thousands separators.

This gets rid of the thousands separator, but also changes the decimal
point to the "C" locale. Right now I'm testing in French (Canada) and
it puts the ',' in place of '.' when I use the basic locale, but if I
try the above it puts '.'.

I guess I'll have to write some sort of wrapper object. No other way?
 
N

Noah Roberts

Noah said:
Jerry said:
This gets rid of the thousands separator, but also changes the decimal
point to the "C" locale. Right now I'm testing in French (Canada) and
it puts the ',' in place of '.' when I use the basic locale, but if I
try the above it puts '.'.

I guess I'll have to write some sort of wrapper object. No other way?

This is what I've come up with, anyone think of a better approach?

template < typename Elem >
struct no_thous_punct : std::numpunct<Elem>
{
typedef typename std::numpunct<Elem>::char_type char_type;
typedef typename std::numpunct<Elem>::string_type string_type;
private:
std::locale loc;
std::numpunct<Elem> const& punct() const { return std::use_facet<
std::numpunct<Elem> >(loc); }
protected:
string_type do_grouping() const { return ""; }
string_type do_falsename() const { return punct().falsename(); }
char_type do_decimal_point() const { return punct().decimal_point(); }
string_type do_truename() const { return punct().truename(); }
char_type do_thousands_sep() const { return punct().thousands_sep(); }

public:
no_thous_punct(std::locale const& l) : loc(l) {}
};
 
J

Jerry Coffin

[ ... ]
This gets rid of the thousands separator, but also changes the decimal
point to the "C" locale. Right now I'm testing in French (Canada) and
it puts the ',' in place of '.' when I use the basic locale, but if I
try the above it puts '.'.

Sorry -- an imcomplete explanation on my part. The idea was to create
everything else in the facet as a copy of the numpunct facet from the
locale you were using otherwise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top