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

问题描述 投票:0回答:9
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;      
}

这是输出

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

我希望数字显示为精确数字而不是科学数字。我该怎么做?

c++ double cout ostream scientific-notation
9个回答
133
投票

使用

std::fixed
流操纵器:

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

40
投票

如上所述,您可以使用

std::fixed
来解决您的问题,像这样:

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

但是,完成此操作后,每次在项目中的任何地方打印

float
double
时,数字仍将以这种固定表示法打印。您可以使用

将其转回
cout << scientific;

但是如果您的代码变得更复杂,这可能会变得乏味。

这就是为什么 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

您可以在官方文档中找到有关 Boost 的 I/O Stream State Saver 的更多信息。

您可能还想查看 Boost 格式库,它也可以使您的输出更容易,尤其是当您必须处理国际化时。但是,它不会帮助您解决这个特定问题。


14
投票

编写下一个语法:

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

其中

(n)
是小数精度的数字。 这应该修复它。

P.s.:您需要

#include <iomanip>
才能使用
std::setprecision
.


8
投票

在 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
投票

您可以使用格式标志

更多信息:http://www.cplusplus.com/reference/iostream/ios_base/fmtflags/


2
投票

你可以这样使用函数 fixed :

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

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

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


1
投票

您可以通过 iostream 获得一整套格式化操作符。这是一个 tutorial 让你开始。


0
投票

您也可以在c++中使用

printf
来打印不带科学计数法的值。

因为您也可以使用

cin
cout
代替
scanf
printf
,但是,如果您将一百万个数字作为输入并打印一百万行,则使用
scanf
会更快printf
.

#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 << " to show exact numbers and not scientific numbers.

  • 这是一个小而简单的修复程序;)

***如果你希望输出保留两位小数,你可以使用这个代码:***

(使用#include 然后输入“<< setprecison(n) << " after " << fixed << ", where 'n' is the number of decimal places you want. So, in your case, you can use 2 for n and get 2 decimal places. The full code is written below.)

#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;      
}
}
© www.soinside.com 2019 - 2024. All rights reserved.