another locale Q: lose thousands sep

Discussion in 'C++' started by Noah Roberts, Apr 11, 2008.

  1. Noah Roberts

    Noah Roberts Guest

    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?
    Noah Roberts, Apr 11, 2008
    #1
    1. Advertising

  2. Noah Roberts

    Jerry Coffin Guest

    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.
    Jerry Coffin, Apr 11, 2008
    #2
    1. Advertising

  3. Noah Roberts

    James Kanze Guest

    On Apr 11, 1:24 am, Noah Roberts <> 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
    James Kanze, Apr 11, 2008
    #3
  4. Noah Roberts

    Noah Roberts Guest

    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?
    Noah Roberts, Apr 11, 2008
    #4
  5. Noah Roberts

    Noah Roberts Guest

    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) {}
    };
    Noah Roberts, Apr 11, 2008
    #5
  6. Noah Roberts

    Jerry Coffin Guest

    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.
    Jerry Coffin, Apr 12, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,819
    Guus Bosman
    Jul 25, 2004
  2. billiejoex

    os.sep and os.path.sep

    billiejoex, Sep 9, 2007, in forum: Python
    Replies:
    2
    Views:
    450
    O.R.Senthil Kumaran
    Sep 10, 2007
  3. Replies:
    4
    Views:
    982
  4. Gabriel Genellina
    Replies:
    0
    Views:
    681
    Gabriel Genellina
    Feb 18, 2009
  5. zade
    Replies:
    1
    Views:
    591
    James Kanze
    Mar 5, 2010
Loading...

Share This Page