Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 문자열
- function
- 티스토리챌린지
- 배열
- Data Science
- 포인터
- raw data
- Class
- predictive analysis
- baekjoon
- 반복문
- C++
- Object Oriented Programming
- 파이썬
- vscode
- programming
- 함수
- pass by reference
- string
- Python
- pointer
- Pre-processing
- const
- Deep Learning
- 백준
- array
- OOP
- assignment operator
- 알고리즘
- 오블완
Archives
- Today
- Total
Channi Studies
[C++] <iomanip> setprecision(n) 본문
iomanip library를 활용하면 실수형 자료를 출력할 때, 소수점 몇번째 자리에서 반올림을 할 지 결정할 수 있습니다.
형식
// FORMAT
# include <iomanip>
cout << fixed << setprecision(num);
예시 코드
// Example Code
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout << fixed << setprecision(1);
cout << 14.889 << endl; // 14.9
cout << fixed << setprecision(2);
cout << 2.192 << endl; // 2.19
cout << fixed << setprecision(0);
cout << 1.199 << endl; // 1
}
setprecision(n)에서의 n은 소수점 이후 몇번째 자릿수에서 반올림을 할 것인지를 결정합니다.
n+1번째 자리에서 반올림을 한다고 생각하면 됩니다.
예를 들어 1이면 소수점 이후 1 + 1 = 2번째 자리에서 반올림이 되서, 14.889 → 14.9
2이면 소수점 이후 2 + 1 = 3번째 자리에서 진행되기 때문에, 2.192 → 2.19
0이면 1번째 자리이므로 1.199 → 1 입니다.
'C++ > 기타' 카테고리의 다른 글
[C++] 추가적인 변수 선언 없이 값 교환하기 (swap) (1) | 2023.12.19 |
---|---|
[C++] 랜덤 숫자 random number (0) | 2023.12.16 |
[C++] Compund Assignment Operator (0) | 2023.12.07 |
[C++] c++에서 boolean 값을 true/false로 출력하고 싶을 때 (0) | 2023.12.07 |
[C++] 정수끼리의 나눗셈 (0) | 2023.12.07 |