본문 바로가기

C++

C++ 출력 포맷

C++은 출력포맷을 맞춰주기 위해 다양한 포맷 입출력 기능을 제공한다.

ios 클래스에 포맷 플래그가 정의되어 있다.
https://msdn.microsoft.com/ko-kr/library/5yc0df6d.aspx 

Manipulators(조작자)는 다음과 같으며, 예를 들어 ios:fixed 와 같은 형태로 사용된다.

boolalpha

Specifies that variables of type bool appear as true or false in the stream.

dec

Specifies that integer variables appear in base 10 notation.

defaultfloat

Configures the flags of an ios_base object to use a default display format for float values.

fixed

Specifies that a floating-point number is displayed in fixed-decimal notation.

hex

Specifies that integer variables appear in base 16 notation.

internal

Causes a number's sign to be left justified and the number to be right justified.

left

Causes text that is not as wide as the output width to appear in the stream flush with the left margin.

noboolalpha

Specifies that variables of type bool appear as 1 or 0 in the stream.

noshowbase

Turns off indicating the notational base in which a number is displayed.

noshowpoint

Displays only the whole-number part of floating-point numbers whose fractional part is zero.

noshowpos

Causes positive numbers to not be explicitly signed.

noskipws

Cause spaces to be read by the input stream.

nounitbuf

Causes output to be buffered and processed when the buffer is full.

nouppercase

Specifies that hexadecimal digits and the exponent in scientific notation appear in lowercase.

oct

Specifies that integer variables appear in base 8 notation.

right

Causes text that is not as wide as the output width to appear in the stream flush with the right margin.

scientific

Causes floating point numbers to be displayed using scientific notation.

showbase

Indicates the notational base in which a number is displayed.

showpoint

Displays the whole-number part of a floating-point number and digits to the right of the decimal point even when the fractional part is zero.

showpos

Causes positive numbers to be explicitly signed.

skipws

Cause spaces to not be read by the input stream.

unitbuf

Causes output to be processed when the buffer is not empty.

uppercase

Specifies that hexadecimal digits and the exponent in scientific notation appear in uppercase.

또한 <iomanip>에는 << 와 >> 연산자와 함께 사용되는 조작자(manipulator)를 제공하는데, https://msdn.microsoft.com/ko-kr/library/ydd54a6t.aspx 다음과 같이 사용된다.

get_money

Obtains a monetary amount, optionally in international format.

get_time

Obtains a time in a time structure by using a specified format.

put_money

Provides a monetary amount, optionally in international format.

put_time

Provides a time in a time structure and a format string to use.

quoted

Enables convenient round-tripping of strings with insertion and extraction operators.

resetiosflags

Clears the specified flags.

setbase

Set base for integers.

setfill

Sets the character that will be used to fill spaces in a right-justified display.

setiosflags

Sets the specified flags.

setprecision

Sets the precision for floating-point values.

setw

Specifies the width of the display field.

 예를 들어 double value 값을 소수점 아래 둘째자리까지 출력하는데, 폭을 5로 맞춘다면

cout.setf(ios::fixed);
cout << "p = " <<  setw(5) << setprecision(2) << value<< endl; 

또는

  // <iomanip> 사용
  cout << fixed << setprecision(3) << "x = " << x << ", y = " << y << endl;

와 같이 사용한다.

 

'C++' 카테고리의 다른 글

C++ 입출력시스템 정리  (0) 2016.07.05
STL vector와 sort  (0) 2016.07.05
STL vector의 사용법  (0) 2016.07.04
enum 과 연산자 중복의 예  (0) 2016.07.04
Template를 사용한 swap 함수  (0) 2016.07.04