我有一个
float
值列表,我想用 cout
打印它们,保留 2 位小数。
例如:
我该怎么做?
(
setprecision
似乎对此没有帮助。)
通过
<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
你就快到了,还需要使用 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
setprecision(n)
适用于整个数字,而不是小数部分。您需要使用定点格式使其适用于小数部分:setiosflags(ios::fixed)
简化已接受的答案
简化示例:
#include <iostream>
#include <iomanip>
int main()
{
double d = 122.345;
std::cout << std::fixed << std::setprecision(2) << d;
}
你会得到输出
122.34
参考:
#include<stdio.h>
int main()
{
double d=15.6464545347;
printf("%0.2lf",d);
}
我在编码比赛中遇到了类似的问题,这就是我处理它的方法。 将所有双精度值的精度设置为 2
首先添加标头以使用set precision
#include <iomanip>
然后在我们的main中添加以下代码
double answer=5.9999;
double answer2=5.0000;
cout<<setprecision(2)<<fixed;
cout <<answer << endl;
cout <<answer2 << endl;
输出:
5.99
5.00
你需要使用fixed来写入5.00,这就是为什么,你的输出不会是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>
#include <cstdio>
#include <iomanip>
using namespace std;
int main() {
int a;
long int b;
char c;
float d;
double e;
cin>>a>>b>>c>>d>>e;
cout<<a<<"\n"<<b<<"\n"<<c<<"\n";
cout<<fixed<<setprecision(3)<<d<<"\n";
cout<<fixed<<setprecision(9)<<e;
return 0;
}
简单的代码一定会对您有帮助!
您必须将“浮动模式”设置为固定。
float num = 15.839;
// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;
带有模板
#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';
}
std::format
来执行此操作:
std::cout << std::format("{:.2f}", 10.900);
输出:
10.90
它比使用 I/O 操纵器更简洁、更高效。它还使您可以控制区域设置,默认情况下与区域设置无关。
免责声明:我是 C++20 的作者
std::format
。
最简单的方法:
#include<stdio.h>
int main() {
double d = 122.345;
printf(".2%lf",d);
}
只是一个小问题;将以下内容放在标题中
使用命名空间 std;
然后
std::cout << std::fixed << std::setprecision(2) << d;
简化为
cout<< fixed << setprecision(2) << d;
这是一个使用矩阵的示例。
cout<<setprecision(4)<<fixed<<m[i][j]