Life, Education, Death

プログラミング以外でも思ったことをつらつらと書きたい

strstreamやboost::lexical_castをしたときに数値にカンマが入る

答えはここにあった!!!
http://www.freeml.com/cppll_novice/1550/latest

std::locale loc( std::locale( "japanese" ), &std::use_facet<
std::numpunct< char > >( std::locale::classic() ) );
std::locale::global( loc );

とか

std::locale loc = std::locale( "japanese" ).combine< std::numpunct< char > >( std::locale::classic() );
std::locale::global( loc );

すると日本語でカンマ区切りでない結果が得られるようだ。

std::locale loc = std::locale();
loc = std::locale(loc, "japanese", std::locale::ctype);
std::locale::global(loc);

と書く場合は文字だけを日本語にするようで、こっちのほうがよい結果が得られた。

今回はこれをするとファイルの入力でこけるので、別にキャスト関数を書いた。

template< typename Source, typename Target >
inline static Target c_locale_lexical_cast( const Source &source )
{
	std::stringstream ss;
	ss::locale("C");
	Target target;
	ss << source;
	ss >> target;
	return target;	
}