본문 바로가기

c++

c++ cout 출력 소수점 정밀도 setprecision, fixed

c에서 소수점 n째 짜리까지 출력하려면 아래와 같이 사용했다

#include <stdio.h>

int main() {
	int a, b;
	scanf_s("%d %d", &a, &b);
	printf("%.3f", (double)a / b);
}

c++에서 소수점 n자리까지 출력하려면 아래 예제와 같이 setprecision(n)과 fixed를 사용하면 된다

#include <bits/stdc++.h>
using namespace std;

int main() {
	int a, b;
	cin >> a >> b;
	cout << fixed << setprecision(3) << ((double)a / b);
}

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

c++11 원시 문자열 리터럴  (0) 2021.10.10