프로그래밍/c++

c++ 천단위마다 콤마(,) 찍기

GaePein 2013. 6. 18. 12:25

ANSI C++도 Java 처럼 천단위 콤마 찍기를 지원합니다.
표준 라이브러리의 locale 클래스를 사용하면 됩니다.

 

std::string sep_thousands(double f)
{
    using namespace std;

    const char *locale_name =
    #ifdef WINDOWS
    "korean";
    #endif
   
    ostringstream oss;
    oss.imbue(locale(locale_name));
    oss << f;
    return oss.str();
}


로케일을 한국어나 영어로 설정하면 자동으로 천단위 컴마 찍기가 됩니다.
(소숫점 이하는 찍히지 않습니다.)
만약 독일어로 설정하면 컴마(,) 대신 피리어드(.)가 찍힙니다.

 

출처 : http://kldp.org/node/38269