답변:
로에게 <iomanip>, 당신이 사용할 수있는 std::fixed및std::setprecision
여기에 예가 있습니다
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << d;
}
그리고 당신은 출력을 얻을 것이다
122.34
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x) . 사용법은 다음과 같습니다.cout<<FIXED_FLOAT(d)
당신은 거의 거기에 있었고 std :: fixed도 사용해야합니다 .http : //www.cplusplus.com/reference/iostream/manipulators/fixed/를 참조하십시오 .
#include <iostream>
#include <iomanip>
int main(int argc, char** argv)
{
float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };
std::cout << std::setprecision(2) << std::fixed;
for(int i = 0; i < 6; ++i)
{
std::cout << testme[i] << std::endl;
}
return 0;
}
출력 :
0.12
1.23
12.35
123.45
1234.50
12345.00
허용 된 답변을 단순화
단순화 된 예 :
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed << std::setprecision(2) << d;
}
그리고 당신은 출력을 얻을 것이다
122.34
참고:
일관된 형식을 원하면서 정수에 문제가있었습니다.
완전성을위한 재 작성 :
#include <iostream>
#include <iomanip>
int main()
{
// floating point formatting example
double d = 122.345;
cout << std::fixed << std::setprecision(2) << d << endl;
// Output: 122.34
// integer formatting example
int i = 122;
cout << std::fixed << std::setprecision(2) << double(i) << endl;
// Output: 122.00
}
코딩 경쟁에서 이와 비슷한 문제가 있었고 이것이 내가 처리 한 방법입니다. 모든 double 값에 정밀도 2를 설정
먼저 setprecision을 사용하기 위해 헤더를 추가하십시오.
#include <iomanip>
그런 다음 메인에 다음 코드를 추가하십시오.
double answer=5.9999;
double answer2=5.0000;
cout<<setprecision(2)<<fixed;
cout <<answer << endl;
cout <<answer2 << endl;
산출:
5.99
5.00
5.00을 쓰기 위해 fixed를 사용해야하므로 출력이 5.00이되지 않습니다.
소수점 뒤에 고정 된 2 자리를 설정하려면 다음을 먼저 사용하십시오.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
그런 다음 이중 값을 인쇄하십시오.
이것은 예입니다 :
#include <iostream>
using std::cout;
using std::ios;
using std::endl;
int main(int argc, char *argv[]) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
double d = 10.90;
cout << d << endl;
return 0;
}
템플릿
#include <iostream>
// d = decimal places
template<int d>
std::ostream& fixed(std::ostream& os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
int main(){
double d = 122.345;
std::cout << fixed<2> << d;
}
너비 옵션도 과학적으로 유사합니다 (열에 유용)
// d = decimal places
template<int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& f(std::ostream &os){
os.setf(std::ios_base::fixed, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
// d = decimal places
template<int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
return os;
}
// w = width, d = decimal places
template<int w, int d>
std::ostream& e(std::ostream &os){
os.setf(std::ios_base::scientific, std::ios_base::floatfield);
os.precision(d);
os.width(w);
return os;
}
int main(){
double d = 122.345;
std::cout << f<10,2> << d << '\n'
<< e<10,2> << d << '\n';
}
이것은 행렬을 사용한 예제입니다.
cout<<setprecision(4)<<fixed<<m[i][j]