473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?
Apr 10 '08 #1
5 2639
In article <ft**********@aioe.org>, us**@example.net 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.
Apr 12 '08 #2
On Apr 11, 1:24 am, Noah Roberts <u...@example.netwrote:
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:ja*********@gmail.com
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
Jun 27 '08 #3
Jerry Coffin wrote:
In article <ft**********@aioe.org>, us**@example.net 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<Tdo_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?
Jun 27 '08 #4
Noah Roberts wrote:
Jerry Coffin wrote:
>In article <ft**********@aioe.org>, us**@example.net 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<Tdo_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<Elemconst& 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) {}
};
Jun 27 '08 #5
In article <ft**********@aioe.org>, us**@example.net 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.
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Timothy Smith | last post by:
hi there i need to set my locale so that when i output a number it's formatted with thousands grouped. the problem i've found is there doesn't seem to be very good cross platform support for...
4
by: Roger Leigh | last post by:
Hello, I'm writing a fixed-precision floating point class, based on the ideas in the example fixed_pt class in the "Practical C++ Programming" book by Steve Oualline (O' Reilly). This uses a...
6
by: Troels Arvin | last post by:
Hello, In the init-script contained in the RPMs downloadable from the PostgreSQL site (I checked the one for Fedora), an explicit locale is set before running initdb. - And the explicit locale...
1
by: Manolatos Tilemahos | last post by:
Dear friends, First pls accept my apologies, because I am a new Postgres user, so my questions could be silly. We need to setup a database using UTF-8 encoding, however, by some way, we have...
0
by: Sally Whitfield | last post by:
8Zcc ----uMyItidGBFcIxNvn7Ubo Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html><head><style type=3Dtext/css>.eyebrow { FONT-WEIGHT: bold; FONT-SIZE= : 10px;...
24
by: Donn Ingle | last post by:
Hello, I hope someone can illuminate this situation for me. Here's the nutshell: 1. On start I call locale.setlocale(locale.LC_ALL,''), the getlocale. 2. If this returns "C" or anything...
3
by: SimonPalmer | last post by:
anyone recommend a way of formatting floats with comma separators? e.g. 500000.00 -500,000.00
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.