C++에서 setprecision을 사용하는 방법

Jinku Hu 2023년10월12일
  1. setprecision()메서드를 사용하여 부동 소수점에 대한 사용자 지정 정밀도 설정
  2. setprecision()std::fixed()를 사용하여 부동 소수점에 대한 사용자 지정 정밀도 설정
  3. setprecision()std::fixed()를 사용하여 부동 소수점을 소수점에 정렬
C++에서 setprecision을 사용하는 방법

이 기사에서는 C++에서setprecision 메서드를 사용하는 방법에 대한 여러 메서드를 보여줍니다.

setprecision()메서드를 사용하여 부동 소수점에 대한 사용자 지정 정밀도 설정

setprecision()은 입력 / 출력 조작기 라이브러리<iomanip>의 일부이며 부동 소수점 숫자의 기본 정밀도를 수정하는 데 사용할 수 있습니다. setprecision()은 일반적으로 I/O 스트림이있는 표현식에서 사용됩니다.

다음 예제는cout 출력 스트림 객체에 부동 소수점 숫자를 설정하는 방법을 보여줍니다. setprecision()은 정수 (정수 부분과 분수 부분)에 적용되며 숫자가 지정된 정밀도보다 큰 크기를 가질 때 과학적 표기법을 사용합니다.

#include <iomanip>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;

int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.324,     0.012,
                          26.9491092019, 11013,  92.001112, 0.000000234};

  for (auto &i : d_vec) {
    cout << setprecision(3) << i << " | ";
  }
  cout << endl;

  return EXIT_SUCCESS;
}

출력:

123 | 2.23 | 0.324 | 0.012 | 26.9 | 1.1e+04 | 92 | 2.34e-07 |

setprecision()std::fixed()를 사용하여 부동 소수점에 대한 사용자 지정 정밀도 설정

또는setprecision()fixed()스트림 조작기를 함께 사용하여 소수점 뒤에 동일한 자릿수로 부동 소수점 값을 인쇄 할 수 있습니다. fixed()메서드는 숫자의 소수 부분을 고정 길이로 설정하며 기본적으로 6 자리입니다. 다음 코드 샘플에서는 cout스트림으로 출력하고 숫자가 출력에 삽입되기 전에 두 조작자를 호출합니다.

#include <iomanip>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;

int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.324,     0.012,
                          26.9491092019, 11013,  92.001112, 0.000000234};

  for (auto &i : d_vec) {
    cout << fixed << setprecision(3) << i << " | ";
  }
  cout << endl;

  return EXIT_SUCCESS;
}

출력:

123.231 | 2.234 | 0.324 | 0.012 | 26.949 | 11013.000 | 92.001 | 0.000 |

setprecision()std::fixed()를 사용하여 부동 소수점을 소수점에 정렬

마지막으로setw,right,setfill,fixedsetprecision 조작자를 결합하여 소수점에 정렬 된 부동 소수점 숫자를 출력 할 수 있습니다. setw 메소드는 인수로 전달 된 문자 수로 출력 스트림 너비를 지정합니다. setfill은 사용하지 않은 공간을 채울 문자를 설정하고right 메소드는cout에게 채우기 작업이 적용되는 쪽을 알려줍니다.

#include <iomanip>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::fixed;
using std::setprecision;
using std::vector;

int main() {
  vector<double> d_vec = {123.231,       2.2343, 0.324,     0.012,
                          26.9491092019, 11013,  92.001112, 0.000000234};

  for (auto &i : d_vec) {
    cout << std::setw(10) << std::right << std::setfill(' ') << fixed
         << setprecision(4) << i << endl;
  }
  cout << endl;

  return EXIT_SUCCESS;
}

출력:

  123.2310
    2.2343
    0.3240
    0.0120
   26.9491
11013.0000
   92.0011
    0.0000
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - C++ Float