用 cout 打印正确的小数点数

我有一个 float值的列表,我想用小数点后2位的 cout打印它们。

例如:

10.900  should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34

我怎么能这么做?

(setprecision似乎对此没有帮助。)

728641 次浏览

使用 <iomanip>,您可以使用 std::fixedstd::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

setprecision(n)适用于整数,而不是小数部分。您需要使用定点格式使其适用于小数部分: setiosflags(ios::fixed)

差不多了,还需要使用 std: : fix,请参考 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

你必须设置“浮动模式”为固定。

float num = 15.839;


// this will output 15.84
std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;

简化公认的答案

简单的例子:

#include <iostream>
#include <iomanip>


int main()
{
double d = 122.345;
std::cout << std::fixed << std::setprecision(2) << d;
}

你会得到输出

122.34

参考文献:

这是一个使用矩阵的例子。

cout<<setprecision(4)<<fixed<<m[i][j]

只是一个小问题,把下面的标题

使用命名空间 std;

那么

计数 < < 标准: : 固定 < 标准: : 设定精度(2) < < d;

简化为

计数器 < < 固定 < 设定精度(2) < d;

要在小数点后设置固定的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<stdio.h>
int main()


{


double d=15.6464545347;


printf("%0.2lf",d);


}

我在一次编程竞赛中遇到过类似的问题,我就是这样处理的。 将所有双精度值的精度设置为2

首先添加消息头以使用 set 精度

#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

你需要为写5.00而使用 fix,这就是为什么,你的输出不会为5.00而来。

一个简短的参考视频链接,我添加这是有帮助的

模板

#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;
}

对于 science 也是类似的,带有宽度选项(对于列很有用)

// 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';
}
#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;
}

简单的代码肯定会对您有所帮助!

最简单的方法:

#include <iostream>
using namespace std;


int main() {
double d = 122.345;


printf(".2%lf",d);
return 0;
}