Noah Roberts wrote:
> Jerry Coffin wrote:
>> In article <ftm7eq$gd3$>, says...
>
>>> 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.
>>
>
> 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) {}
};