another locale Q: lose thousands sep

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noah Roberts

    another locale Q: lose thousands sep

    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?
  • Jerry Coffin

    #2
    Re: another locale Q: lose thousands sep

    In article <fto3o7$lhn$1@a ioe.org>, user@example.ne t 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.

    Comment

    • James Kanze

      #3
      Re: another locale Q: lose thousands sep

      On Apr 11, 1:24 am, Noah Roberts <u...@example.n etwrote:
      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:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • Noah Roberts

        #4
        Re: another locale Q: lose thousands sep

        Jerry Coffin wrote:
        In article <ftm7eq$gd3$1@a ioe.org>, user@example.ne t 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_stri ng<Tdo_grouping () const { return ""; }
        };
        >
        Create your locale like this:
        >
        std::locale no_sep(your_loc ale, 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?

        Comment

        • Noah Roberts

          #5
          Re: another locale Q: lose thousands sep

          Noah Roberts wrote:
          Jerry Coffin wrote:
          >In article <ftm7eq$gd3$1@a ioe.org>, user@example.ne t 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_stri ng<Tdo_grouping () const { return ""; }
          >};
          >>
          >Create your locale like this:
          >>
          >std::locale no_sep(your_loc ale, 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<E lem>
          {
          typedef typename std::numpunct<E lem>::char_type char_type;
          typedef typename std::numpunct<E lem>::string_ty pe string_type;
          private:
          std::locale loc;
          std::numpunct<E lemconst& punct() const { return std::use_facet<
          std::numpunct<E lem(loc); }
          protected:
          string_type do_grouping() const { return ""; }
          string_type do_falsename() const { return punct().falsena me(); }
          char_type do_decimal_poin t() const { return punct().decimal _point(); }
          string_type do_truename() const { return punct().truenam e(); }
          char_type do_thousands_se p() const { return punct().thousan ds_sep(); }

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

          Comment

          • Jerry Coffin

            #6
            Re: another locale Q: lose thousands sep

            In article <fto3o7$lhn$1@a ioe.org>, user@example.ne t 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.

            Comment

            Working...