반응형
1. 'std::fixed' 와 'std::setprecision'
- 소수점 아래의 자리수를 정확하게 지정할 수 있다.
- '<iomanip>' 해더 파일에 있다.
double num = 3.14159265;
cout << fixed << setprecision(3) << num << endl;
//출력: 3.142
- 'std::'는 생략한다.
2. 'std::stringstream' 와 'std::setprecision'
- 'std::strignstream' 으로 숫자를 문자열로 바꾼다.
- 'std::setprecision' 으로 소수점 자리를 지정해준다.
- '<iomanip>' 해더 파일에 있다.
double num = 3.14159265;
stream << fixed << setprecision(4) << num;
string s = stream.str();
cout << s << endl;
//출력: 3.1416
- 'std::'는 생략한다.
3. 'std::scientific' 와 'std::setprecision'
- 소수점 아래의 자리수를 과학적인 표기법으로 출력할 수 있다.
- '<iomanip>' 해더 파일에 있다.
double num = 1234.56789;
cout << scientific << setprecision(2) << num << endl;
//출력: 1.23e+03
- 'std::'는 생략한다.
4. 'std::cout.precision()'
- 함수 호출후 소수점 아래 자릿수를 전체적으로 설정한다.
double num = 3.14159265;
cout.precision(3);
cout << fixed << num << endl;
//출력: 3.142
- 'std::'는 생략한다.
끝
반응형
'c++ > c++' 카테고리의 다른 글
[c++] sort (1) | 2025.03.02 |
---|---|
[c++] 팩토리얼 계산하기 (0) | 2024.04.28 |
[c++] (0) | 2024.02.18 |
[c++] #include <bits/stdc++.h> (0) | 2024.02.18 |
[c++] using namespace std (0) | 2024.02.15 |