C++ 如何制作C++;不能使用科学符号 double x=1500; 对于(int k=0;k

C++ 如何制作C++;不能使用科学符号 double x=1500; 对于(int k=0;k,c++,double,cout,ostream,scientific-notation,C++,Double,Cout,Ostream,Scientific Notation,可以使用格式标志 更多信息:您可以从iostream获得一整套格式化运算符。下面是一个示例,让您开始使用。使用流操纵器: 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: &quo

可以使用格式标志


更多信息:

您可以从iostream获得一整套格式化运算符。下面是一个示例,让您开始使用。

使用流操纵器:

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

cout如上所述,您可以使用
std::fixed
解决您的问题,如下所示:

cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
cout << scientific;
但如果代码变得更复杂,这可能会变得单调乏味

这就是Boost制作I/O流状态保护程序的原因,它会自动将您正在使用的I/O流返回到函数调用之前的状态。您可以这样使用它:

cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
cout << scientific;
#include//您需要先下载这些标题
{
boost::io::ios\U标志\U保护程序ifs(操作系统);
cout编码下一个语法:

#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

std::cout您可以通过以下方式使用函数fixed


STD::CUT< P>,也可以在C++中使用<代码> Prtff<代码>在没有科学符号的值中打印。 由于您也可以使用

cin
cout
而不是
scanf
printf
,但是,如果您输入一百万个数字并打印一百万行,则使用
scanf
printf
会更快

#包括
#包括
int main(){
双a=功率(2,99);
printf(“%lf\n”,a);
返回0;

}
在C++20中,您可以使用来执行以下操作:

std::cout << std::fixed << std::setprecision(n);
这种方法的优点是它不会改变流状态

同时,您可以使用,
std::format
是基于。{fmt}还提供了
print
功能,使这一过程更加简单和高效():


免责声明:我是{fmt}和C++20
std::format

的作者,为什么您要将
double
转换为
double
?在使用时,ostream中可能存在防止科学符号重复的情况
fmt::print("Bas ana: {:f}\tSon faiz: {:f}\tSon ana: {:f}\n", x, t, x + t);