[ C++ 소수점 출력하기 - cout.setprecision() ]
cout.precision()은 사실 소수점 아래 부분의 출력 범위만 설정하는 게 아니고 실수의 정수부와 소수부를 합친, 전체의 출력 범위를 설정하는 함수입니다. 만약, 이걸 소수점 아래 숫자의 출력 범위만 설정하게 쓰려면 함수 위에 cout << fixed; 라는 라인을 추가합니다. 이걸 설정한 후 다시 해제해서 실수 전체의 출력 범위를 설정하고 싶다면 cout.unsetf(ios::fixed); 이라는 라인을 추가해주면 됩니다 |
#include<iostream>
using namespace std;
int main(){
double a,b;
cin>>a>>b;
cout<<fixed; //주석처리해보고 실행시켜 보세요
cout.precision(15);
cout<<a/b;
}
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
int a = 100;
int b = 3;
double c = 123.3472;
cout << fixed << setprecision(2) << c << '\n';
cout.unsetf(ios::fixed);
cout << endl;
cout.precision(4);
cout << a/(double)b << endl;
cout << c << endl;
cout << endl;
cout << fixed;
cout.precision(4);
cout << a/(double)b << endl;
cout << c << endl;
cout << endl;
cout.unsetf(ios::fixed);
cout << a/(double)b << endl;
cout << c << endl;
cout << endl;
cout << "ceil(3.8) = " << ceil(3.8) << endl; //정수로 올림
cout << "ceil(4.2) = " << ceil(4.2) << endl;
cout << "floor(3.8) = " << floor(3.8) << endl; //정수로 내림
cout << "floor(3.1) = " << floor(3.1) << endl;
cout << "round(5.2) = " << round(5.2) << endl; //정수로 반올림
cout << "round(5.6) = " << round(5.6) << endl;
}
'C++ 언어' 카테고리의 다른 글
7. C/C++ 단순 연결 리스트 (0) | 2021.01.28 |
---|---|
6. C/C++ 연결리스트 (0) | 2021.01.18 |
5. C/C++ 함수 기초 (0) | 2020.11.23 |
4. C/C++ 동적 메모리 (0) | 2020.11.05 |
3. C/C++ 연산자 (0) | 2020.09.25 |