如何让 C++ 的 cout 不使用科学计数法

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

这是输出结果:

基本金额:3284.78 最终利息:1784.78 最终金额:5069.55

基本金额:7193.17 最终利息:3908.4 最终金额:11101.6

基本金额:15752 最终利息:8558.8 最终金额:24310.8

基本金额:34494.5 最终利息:18742.5 最终金额:53237

基本金额:75537.8 最终利息:41043.3 最终金额:116581

基本金额:165417 最终利息:89878.7 最终金额:255295

基本金额:362238 最终利息:196821 最终金额:559059

基本金额:793246 最终利息:431009 最终金额:1224260

基本金额:1737090 最终利息:943845 最终金额:2680940

基本金额:3803970 最终利息:2066880 最终金额:5870850

我希望以精确数字的形式显示数字,而不是科学计数法。我该怎么做?


9
你为什么把一个 double 转换成另一个 double - Fred Foo
2
可能是Prevent scientific notation in ostream when using << with double的重复问题。 - Mechanical snail
这个回答解决了你的问题吗?在使用<<和double时如何防止科学计数法在ostream中显示 - undefined
9个回答

137

使用std::fixed流操纵器:

cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;

42

如上所述,您可以使用std::fixed来解决您的问题,例如:

cout << fixed;
cout << "Bas ana: " << x << "\tSon faiz: " << t << "\tSon ana: " << x+t <<endl;

然而,完成这个步骤后,你的项目无论在哪里打印一个float或者double,数字都会以这种固定的表示法输出。你可以通过使用

cout << scientific;

但是如果你的代码变得更加复杂,这可能会变得很繁琐。

这就是为什么Boost创建了I/O流状态保存器,它可以自动将你正在使用的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

您可以在官方文档中了解有关Boost I/O Stream State Saver的更多信息。您还可以查看Boost Format库,它也可以使输出更加简便,特别是在处理国际化时。但是,它对于此特定问题无济于事。请参见:官方文档Boost Format库。保留HTML标记。

18

编写下一语法:

std::cout << std::fixed << std::setprecision(n);

(n)代表小数的精度。

这样应该就解决了。

P.s.: 你需要#include <iomanip>以便使用std::setprecision


9
在C++20中,您将能够使用std::format来实现此功能:
std::cout << std::format("Bas ana: {:f}\tSon faiz: {:f}\t"
                         "Son ana: {:f}\n", x, t, x + t);

输出:

Bas ana: 3284.776791    Son faiz: 1784.776791   Son ana: 5069.553581
Bas ana: 7193.172376    Son faiz: 3908.395585   Son ana: 11101.567961
...

这种方法的优点在于它不会改变流状态。
与此同时,您可以使用基于其构建的{fmt}库std::format。{fmt}还提供了print函数,使这一过程更加简单高效(godbolt)。
fmt::print("Bas ana: {:f}\tSon faiz: {:f}\tSon ana: {:f}\n", x, t, x + t);

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


3

2
您可以这样使用函数fixed

std::cout << std::fixed << ...

或者您可以使用标准格式,例如“12.12%f”的printf函数来使用:

printf("number = %12.12f", float_number);


1

在 iostream 中,有一整套的格式化操作符可供使用。这里有一个tutorial可以帮助你入门。


0

在C++中,您还可以使用printf来打印值而不使用科学计数法。

您也可以使用cincout代替scanfprintf,但是,如果您要输入一百万个数字并打印一百万行,则使用scanfprintf更快。

#include<stdio.h>
#include<math.h>

  int main () { 
  double a = pow(2,99); 
  printf ("%lf\n",a); 
  return 0; 
  }

输出

633825300114114700748351602688.000000


0
请使用以下代码:
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: "<< fixed << x <<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;      
}
  • 您可以使用<< fixed <<来显示精确数字而不是科学计数法。

  • 这是一个小的、简单的修复;)

***如果您想要输出保留2位小数,您可以使用以下代码:***

(使用#include <iomanip>,然后输入<< setprecison(n) << " after " << fixed <<,其中'n'是您想要的小数位数。所以,在您的情况下,您可以使用2作为n并获得2个小数位。完整的代码如下所示。)

#include<iostream>
#include<iomanip>

using namespace std;

int main() {
  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: " << fixed << setprecision(2) << x <<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<< endl;      
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接