class nump: public std::numpunct<char>{
protected:
virtual char do_thousands_sep() const {return ',';}
};
int main()
{
using namespace std;
ostringstream out;
typedef ostreambuf_iterator<char,char_traits<char> > iter_type;
//vc++, same as locale l(out.getloc(), new nump);
locale loc=_ADDFAC(out.getloc(), new nump);
long ulval = 123288;
iter_type begin(out);
const num_put<char,iter_type>& np =
use_facet(loc,(num_put<char,iter_type>*)0,1);
np.put(begin,out,' ',ulval);
cout<<out.str();
return 0;
}
I don't understand how to get the 1,000s separators!
Thank you for any help
Sent via Deja.com http://www.deja.com/
Before you buy.
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
> Can someone please help me to format the number with thousands
> separator? Locale is very confusing and there is almost no help or
> examples on it!
this works;
#include <iostream>
#include <locale>
#include <string>
using namespace std;
int main()
{
locale loc = cout.getloc(); // get current locale.
class my_punct:public std::locale::numpunct<char>
{
protected:
char do_thousands_sep() const {return ',';}
std::string do_grouping() const {return "\3";}
public:
my_punct():numpunct<char>(){}
};
locale new_loc(loc,new my_punct);
cout.imbue(new_loc); // use new_loc
cout <<1000000 << endl;
cout.imbue(loc); // restore the locale to normal
This stuff is addressed in "The C++ Standard Library", Nicolai Josuttis,
Addison-Wesley, like the whole other standard library is.
The approach is to replace the 'numpunct' facet with one where the
'grouping()' matches the desired pattern for the separators and the
'thousands_sep()' matches the desired separation character.
> class nump: public std::numpunct<char>{
> protected:
> virtual char do_thousands_sep() const {return ',';}
> };
This one overrides the functions which actually has the correct value
by default: This is what the default implementation of this function
looks like. However, the default pattern for the thousands separators
is to have no thousands separators. To get thousands separators you have
to change this pattern. This is done by overriding 'do_grouping()'.
Probably this is what you want to have:
class nump: public std::numpunct<char> {
std::string do_grouping() const { return "\03"; }
};
This tells the formatter that after groups of three digits a thousands
separators is to be inserted.
> int main()
> {
> using namespace std;
> ostringstream out;
> typedef ostreambuf_iterator<char,char_traits<char> > iter_type;
> //vc++, same as locale l(out.getloc(), new nump);
> locale loc=_ADDFAC(out.getloc(), new nump);
> long ulval = 123288;
> iter_type begin(out);
> const num_put<char,iter_type>& np =
> use_facet(loc,(num_put<char,iter_type>*)0,1);
Yuk! This is not standard conforming syntax! Although it *is* possible
to get standard conforming sytnax with VC++ (even with VC++ 5.0 although
it does not support explicitly specified template arguments for
function templates). Anyway, why don't you just use 'imbue()' and the
standard formatting functions? This makes things much easier:
out.imbue(loc);
out << 123288;
std::cout << out.str() << "\n";
--
<mailto:dietma...@claas-solutions.de>
homepage: <http://www.informatik.uni-konstanz.de/~kuehl>
Sent via Deja.com http://www.deja.com/
Before you buy.
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
> Can someone please help me to format the number with thousands
> separator? Locale is very confusing and there is almost no help or
> examples on it!
Have a look at the Locale appendix posted on my homepages as part of
the information related to "The C++ Programming Language (3rd Edition)".
It has explanations and examples of locales and facets.
cbar...@ix.netcom.com (Carl Barron) helpfully posted this example:
> this works;
> #include <iostream>
> #include <locale>
> #include <string>
>
> using namespace std;
> int main()
> {
> locale loc = cout.getloc(); // get current locale.
> class my_punct:public std::locale::numpunct<char>
> {
> protected:
> char do_thousands_sep() const {return ',';}
> std::string do_grouping() const {return "\3";}
> public:
> my_punct():numpunct<char>(){}
> };
> locale new_loc(loc,new my_punct);
>
> cout.imbue(new_loc); // use new_loc
>
> cout <<1000000 << endl;
> cout.imbue(loc); // restore the locale to normal
> }
- Bjarne
Bjarne Stroustrup - http://www.research.att.com/~bs