본문 바로가기

Programing/C/C++

C++ 출력 정렬하기(setw, seft, setprecision)

C++ 출력 정렬하기(setw, seft, setprecision)





#include <iostream>

#include <iomanip>


void main()

{

double x=3.1415;

cout<<x<<endl;

cout<<setprecision(3)<<x<<endl;

char str[]="string";


cout.setf(ios::left);                     //좌측 정렬

cout<<setw(10)<<str<<endl;

cout.seft(ios::right);                       //우측 정렬

cout<<setw(10)<<str<<endl;

}


출력 결과


3.1415

3.142

string

    string  





setw(int n)                 데이터가 출력된 화면의 폭을 n으로 지정한다.

setprecision(int n) 실수의 유효숫자 자리수를 지정한다. 지정한 유효숫자 자리에                 

                                서 반올림된다.



출처 : http://blog.naver.com/w2dragon/60013365116