Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > another locale Q: lose thousands sep

Reply
Thread Tools

another locale Q: lose thousands sep

 
 
Noah Roberts
Guest
Posts: n/a
 
      04-10-2008
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?
 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      04-11-2008
In article <ftm7eq$gd3$>, says...
> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      04-11-2008
On Apr 11, 1:24 am, Noah Roberts <u...@example.net> wrote:
> 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.)

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      04-11-2008
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?
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      04-11-2008
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) {}
};
 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-12-2008
In article <fto3o7$lhn$>, says...

[ ... ]

> 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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
os.sep and os.path.sep billiejoex Python 2 09-10-2007 12:22 AM
You lose some, and then you lose some ... Lawrence D'Oliveiro NZ Computing 13 09-24-2006 08:29 AM
VPN 3030, PPTP and SEP vs SEP-E Sam Wilson Cisco 11 02-15-2006 10:36 AM
i18n problem, involving Locale.getDisplayLanguage and Locale.getDisplayCountry Maurice Hulsman Java 1 07-25-2004 06:11 PM
locale.nl_langinfo(RADIXCHAR) vs locale.localeconv()['decimal_point'] Jeff Epler Python 2 08-31-2003 02:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57