如何使 C++ cout 不使用科学记数法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5212018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make C++ cout not use scientific notation
提问by Yunus Eren Güzel
double x = 1500;
for(int k = 0; k<10 ; k++){
double t = 0;
for(int i=0; i<12; i++){
t += x * 0.0675;
x += x * 0.0675;
}
cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
}
this the output
这是输出
Bas ana: 3284.78 Son faiz: 1784.78 Son ana: 5069.55
Bas ana: 7193.17 Son faiz: 3908.4 Son ana: 11101.6
Bas ana: 15752 Son faiz: 8558.8 Son ana: 24310.8
Bas ana: 34494.5 Son faiz: 18742.5 Son ana: 53237
Bas ana: 75537.8 Son faiz: 41043.3 Son ana: 116581
Bas ana: 165417 Son faiz: 89878.7 Son ana: 255295
Bas ana: 362238 Son faiz: 196821 Son ana: 559059
Bas ana: 793246 Son faiz: 431009 Son ana: 1.22426e+006
Bas ana: 1.73709e+006 Son faiz: 943845 Son ana: 2.68094e+006
Bas ana: 3.80397e+006 Son faiz: 2.06688e+006 Son ana: 5.87085e+006
Bas ana:3284.78 Son faiz:1784.78 Son ana:5069.55
Bas ana:7193.17 Son faiz:3908.4 Son ana:11101.6
Bas ana:15752 Son faiz:8558.8 Son ana:24310.8
Bas ana:34494.5 Son faiz:18742.5 Son ana:53237
Bas ana:75537.8 Son faiz:41043.3 Son ana:116581
Bas ana:165417 Son faiz:89878.7 Son ana:255295
Bas ana:362238 Son faiz:196821 Son ana:559059
Bas ana:793246 Son faiz:431009 Son ana:1.22426e+006
Bas ana:1.73709e+006 Son faiz:943845 Son ana:2.68094e+006
Bas ana:3.80397e+006 Son faiz:2.06688e+006 Son ana:5.87085e+006
I want numbers to be shown with exact numbers not scientific numbers. How can I do this?
我希望数字显示准确的数字而不是科学数字。我怎样才能做到这一点?
回答by Tugrul Ates
Use std::fixed
stream manipulator:
使用std::fixed
流操纵器:
cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
回答by Migi
As mentioned above, you can use std::fixed
to solve your problem, like this:
如上所述,您可以使用std::fixed
来解决您的问题,如下所示:
cout << fixed;
cout << "Bas ana: " << x << "\tSon faiz: " << t << "\tSon ana: " << x+t <<endl;
However, after you've done this, every time you print a float
or a double
anywhere in your project, the number will still be printed in this fixed notation. You could turn it back by using
但是,完成此操作后,每次在项目中float
的double
任何位置打印 a或 a 时,数字仍会以这种固定表示法打印。你可以通过使用把它转回来
cout << scientific;
but this might become tedious if your code gets more complicated.
但是如果您的代码变得更复杂,这可能会变得乏味。
This is why Boost made the I/O Stream State Saver, which automatically returns the I/O stream you're using to the state it was before your function call. You can use it like this:
这就是 Boost 制作I/O Stream State Saver 的原因,它会自动将您正在使用的 I/O 流返回到函数调用之前的状态。你可以这样使用它:
#include <boost/io/ios_state.hpp> // you need to download these headers first
{
boost::io::ios_flags_saver ifs( os );
cout << ios::fixed;
cout<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
} // at this bracket, when ifs goes "out of scope", your stream is reset
You can find more info about Boost's I/O Stream State Saver in the official docs.
您可以在官方文档中找到有关 Boost 的 I/O 流状态保护程序的更多信息。
You may also want to check out the Boost Format librarywhich can also make your outputting easier, especially if you have to deal with internationalisation. However, it won't help you for this particular problem.
您可能还想查看Boost Format 库,它也可以使您的输出更容易,尤其是在您必须处理国际化的情况下。但是,它不会帮助您解决此特定问题。
回答by Ahmad Mhd Yones
code the next syntax:
编码下一个语法:
std::cout << std::fixed << std::setprecision(n);
where (n)
is the number of decimal precision.
This should fix it.
其中(n)
是十进制精度的数量。这应该解决它。
回答by muhoweb
You can use format flags
您可以使用格式标志
More info: http://www.cplusplus.com/reference/iostream/ios_base/fmtflags/
更多信息:http: //www.cplusplus.com/reference/iostream/ios_base/fmtflags/
回答by Charlie Martin
回答by zig razor
You can use the function fixedin this way :
您可以使用这种方式修复的功能:
std::cout << std::fixed << ...
std::cout << std::fixed << ...
Or you can use the the printf function with a standard format like "12.12%f" in this way :
或者,您可以通过这种方式使用具有标准格式(如“12.12%f”)的 printf 函数:
printf ("number = %12.12f", float_number);
printf ("number = %12.12f", float_number);