[C++] 출력형식 지정(Output Formatting)

Date:     Updated:

카테고리:

태그:

이 글은 C++ 출력형식 지정(Output Formatting)을 공부하고 정리한 글입니다

int num = 20;
cout << showbase << hex << num << endl;

조정자

  • C++ 출력에서 C보다 더 나은 방법이 없을까 해서 도입한게 조정자(Manipulator)이다

#include <iostream>

showpos / noshowpos

  • num이 123 이라면
    cout << showpos << number;    // +123
    cout << noshowpos << number;  // 123
    

dec / hex / oct

  • num이 123 이라면
    cout << dec << number;  // 123
    cout << hex << number;  // 7b
    cout << oct << number;  // 173
    

uppercase / nouppercase

  • num이 123 이라면
    cout << uppercase << hex << number;     // 7B
    cout << nouppercase << hex << number;   // 7b
    

showbase / noshowbase

  • num이 123 이라면
    cout << showbase << hex << number;    // 0x7b
    cout << noshowbase << hex << number;  // 7b
    

left / internal / right

  • num이 -123 이라면 (공백은 _로 표시했음)
    cout << setw(6) << left << number;      //-123__
    cout << setw(6) << internal << number;  //-__123
    cout << setw(6) << right << number;     //__-123
    

showpoint / noshowpoint

  • decimal1이 100.0, decimal2가 100.12 이라면
    cout << noshowpoint << decimal1 << " " << decimal2;   // 100 100.12
    cout << showpoint << decimal1 << " " << decimal2;     // 100.000 100.120
    

fixed / scientific

  • num이 123.456789 이라면
    cout << fixed << number;        // 123.456789
    cout << scientific << number;   // 1.2345678E+02
    

boolalpha / noboolalpha

  • bReady가 true 라면
    cout << boolalpha << bReady;        // true
    cout << noboolalpha << bReady;      // 1
    

#include <iomanip>

setw()

  • num이 123이라면 (공백은 _로 표시했음)
    cout << setw(6) << num;        // ___123
    

setfill()

  • num이 123이라면
    cout << setfill('*') << setw(6) << num;        // ***123
    

setprecision()

  • num이 123.456789이라면
    cout << setprecision(7) << num;        // 123.4568
    


참조

포큐아카데미 C++ 언매니지드 프로그래밍



💻 열심히 공부해서 작성 중이니 오류나 틀린 부분이 있을 경우 
  언제든지 댓글 혹은 메일로 알려주시면 감사하겠습니다! 😸

맨 위로 이동하기

Cpp 카테고리 내 다른 글 보러가기

댓글 남기기