bruceteen的方法(使用一个如此设置的local):
#include <iostream>
#include <locale>
int main( void )
{
using namespace std;
cout << 1389992 << endl; // 1389992
locale chs( "chs" ); // chs在我这里行,不知道你那里怎么样
cout.imbue( chs );
cout << 1389992 << endl; // 1,389,992
}
特别推荐 namtso的方法(自己订制local):
#include <iostream>
#include <string>
#include <locale>
using namespace std;
class thousands_sep_facet:public std::numpunct<char>
{
public:
explicit thousands_sep_facet( size_t r=0 ) : std::numpunct<char>(r)
{
}
protected:
string do_grouping() const
{
return "\003";
}
};
int main( void )
{
cout << 1389992 << endl; // 1389992
locale loc( locale(), new thousands_sep_facet );
std::cout.imbue( loc );
cout << 1389992 << endl; // 1,389,992
return 0;
}
评论