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

发布于 2024-10-20 16:48:41 字数 947 浏览 3 评论 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: "<<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

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

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

I want numbers to be shown with exact numbers not scientific numbers. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

北陌 2024-10-27 16:48:42

您还可以在 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

You can also use printf in c++ to print in the value without scientific notation.

As You can also usecin and cout instead of scanf and printf, however, if you are taking a million numbers as input and printing a million lines, it is faster to use scanf and printf.

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

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

Output

633825300114114700748351602688.000000

过度放纵 2024-10-27 16:48:42

使用此代码:

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;      
}
  • 您可以使用<<修复了 << 以显示精确数字而不是科学数字。

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

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

(使用 #include,然后输入 << setprecison(n) << " after " << 固定 << ,其中 '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;      
}
}

Use this code:

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;      
}
  • You can use << fixed << to show exact numbers and not scientific numbers.

  • It is a small, easy fix ;)

***If you want the ouput to be in 2 decimal places, you can use this code: ***

(Use #include <iomanip> and then type << 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;      
}
}
荭秂 2024-10-27 16:48:41

使用 std::fixed 流操纵器:

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

Use std::fixed stream manipulator:

cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;
零度° 2024-10-27 16:48:41

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

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

但是,完成此操作后,每次打印 float 或 a 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 格式库 这也可以使您的输出更容易,特别是当您必须处理国际化时。但是,它不会帮助您解决这个特定问题。

As mentioned above, you can use std::fixed to solve your problem, like this:

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

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:

#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.

You may also want to check out the Boost Format library which can also make your outputting easier, especially if you have to deal with internationalisation. However, it won't help you for this particular problem.

看春风乍起 2024-10-27 16:48:41

编写下一个语法:

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

其中 (n) 是小数精度。
这应该可以解决它。

Ps:您需要 #include 才能使用 std::set precision

code the next syntax:

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

where (n) is the number of decimal precision.
This should fix it.

P.s.: you need to #include <iomanip> in order to use std::setprecision.

孤独患者 2024-10-27 16:48:41

在 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 的作者。

In C++20 you'll be able to use std::format to do this:

std::cout << std::format("Bas ana: {:f}\tSon faiz: {:f}\t"
                         "Son ana: {:f}\n", x, t, x + t);

Output:

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

The advantage of this approach is that it doesn't change the stream state.

In the meantime you can use the {fmt} library, std::format is based on. {fmt} also provides the print function that makes this even easier and more efficient (godbolt):

fmt::print("Bas ana: {:f}\tSon faiz: {:f}\tSon ana: {:f}\n", x, t, x + t);

Disclaimer: I'm the author of {fmt} and C++20 std::format.

机场等船 2024-10-27 16:48:41

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

std::cout << std::固定<< ...

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

printf("数字=%12.12f",float_number);

You can use the function fixed in this way :

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

Or you can use the the printf function with a standard format like "12.12%f" in this way :

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

云朵有点甜 2024-10-27 16:48:41

您可以通过 iostream 获得一整套格式化运算符。这是一个教程来帮助您入门。

There are a whole collection of formatting operators that you get with iostream. Here's a tutorial to get you started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文