원하는 형식에 따라 다릅니다. 고정 소수점 자리를 들어, 무엇인가 :
class FFmt
{
int myWidth;
int myPrecision;
public:
FFmt(int width, int precision)
: myWidth(width)
, myPrecision(precision)
{
}
friend std::ostream& operator<<(
std::ostream& dest,
FFmt const& fmt)
{
dest.setf(std::ios::fixed, std::ios::floatfield);
dest.precision(myPrecision);
dest.width(myWidth);
}
};
트릭을 할해야한다, 그래서 당신은 쓸 수 있습니다 :
file << nume << '\t' << FFmt(8, 2) << max ...
(또는 무엇이든 폭 및 정밀 당신이 원하는).
부동 소수점 연산을 수행하는 경우, 많은 경우에 논리적 조작자를 사용하는 것이 더 적절할 것이지만 ( ) 데이터의 형식, 예 : 학위, 거리 등).
IMHO, 조작자를 확장하여 형식화 상태를 저장하고 전체 표현식의 끝에서 복원 할 가치가 있습니다. (모든 manipulator는 이것을 처리하는 기본 클래스에서 파생됩니다.)
[C++ 스트림 출력과 소수점 이하 3 자리 숫자의 가능한 중복. 어떻게?] (http://stackoverflow.com/questions/8554441/c-stream-output-with-3-digits-after-the-decimal-point-how) –