Engineering/C++
[C++] cout.width() 출력할 값의 길이 지정
spiraea
2016. 5. 27. 15:33
printf 함수는 %5d, %2f 등으로 출력할 숫자의 자릿수를 지정할 수 있다.
반면 cout은 그 방법이 묘연했는데, 다음과 같은 함수를 사용할 수 있다는 것을 발견.
cout.width(number)
예)
#include <iostream>
using namespace std;
int main()
{
cout.width(5);
cout<<1.23578797;
}
출력: 1.23579
cout.width를 선언한 바로 다음 한 번의 출력에만 적용된다.