【问题标题】:How to use setprecision in C++如何在 C++ 中使用 setprecision
【发布时间】:2014-03-19 18:55:53
【问题描述】:

我是 C++ 的新手,我只想输出最多 2 位数的积分。 就像如果数字是3.444,那么输出应该是3.44,或者如果数字是99999.4234,那么输出应该是99999.42,我该怎么做。价值是动态的。这是我的代码。

#include <iomanip.h>
#include <iomanip>
int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
}

但它给了我一个错误,未定义的固定符号。

【问题讨论】:

  • 第二行开头的空格,去掉。
  • 如果您使用标准命名空间,则需要包含 using namespace std;

标签: c++


【解决方案1】:
#include <iomanip>
#include <iostream>

int main()
{
    double num1 = 3.12345678;
    std::cout << std::fixed << std::showpoint;
    std::cout << std::setprecision(2);
    std::cout << num1 << std::endl;
    return 0;
}

【讨论】:

  • 函数 main() 中的类名错误 NONAME00.CPP 7:缺少语句;在函数 main() 中错误 NONAME00.CPP 8:类型限定符“std”必须是函数 main() 中的结构或类名称错误 NONAME00.CPP 8:缺少语句;在函数 main() 中错误 NONAME00.CPP 9:类型限定符“std”必须是函数 main() 中的结构或类名称错误 NONAME00.CPP 9:缺少语句;在函数 main() 中警告 NONAME00.CPP 10: 函数应该在函数 main() 中返回一个值 警告 NONAME00.CPP 10: 'num1' 被分配了一个从未在函数 main() 中使用过的值....我正面临这个错误
  • 您确定从答案中逐字复制包含语句吗?
  • @Pradoot 仅供参考,int main() 可以不带返回值,此时默认返回 0。
  • 如果我不想参加比赛。只想将num1 的精度设置为小数点后三位。我该怎么做?
  • @Game_Of_Threads 你不能。哪有这回事。 double 的精度是固定的;它是数据类型的固有部分。我们在这里所做的是在打印供人类消费的数字时改变表观精度。
【解决方案2】:
#include <iostream>
#include <iomanip>
using namespace std;

为方便起见,您可以输入using namespace std; 行。否则,每次您希望使用 coutfixedshowpointsetprecision(2)endl 时,都必须显式添加 std::

int main()
{
    double num1 = 3.12345678;
    cout << fixed << showpoint;
    cout << setprecision(2);
    cout << num1 << endl;
return 0;
}

【讨论】:

    【解决方案3】:
    std::cout.precision(2);
    std::cout<<std::fixed;
    

    当你使用运算符重载时试试这个。

    【讨论】:

      【解决方案4】:

      上面的答案是绝对正确的。这是它的 Turbo C++ 版本。

      #include <iomanip.h>
      #include <iostream.h>
      
      void main()
      {
          double num1 = 3.12345678;
          cout << setiosflags(fixed) << setiosflags(showpoint);
          cout << setprecision(2);
          cout << num1 << endl;
      }
      

      对于fixedshowpoint,我认为应该使用setiosflags函数。

      【讨论】:

      • 您可以改用std::fixedstd::showpoint,就像其他答案所示。但是,如果你要使用std::setioflags(),至少将这些标志组合在一起,例如:cout &lt;&lt; setiosflags(fixed | showpoint) &lt;&lt; ...
      【解决方案5】:

      替换这些标题

      #include <iomanip.h>
      #include <iomanip>
      

      有了这些。

      #include <iostream>
      #include <iomanip>
      using namespace std;
      

      就是这样……!!!

      【讨论】:

        【解决方案6】:

        以下代码运行正确。

        #include <iostream>
        #include <iomanip>
        using namespace std;
        
        int main()
        {
            double num1 = 3.12345678;
            cout << fixed << showpoint;
            cout << setprecision(2);
            cout << num1 << endl;
        }
        

        【讨论】:

          【解决方案7】:
          #include <bits/stdc++.h>                        // to include all libraries 
          using namespace std; 
          int main() 
          { 
          double a,b;
          cin>>a>>b;
          double x=a/b;                                 //say we want to divide a/b                                 
          cout<<fixed<<setprecision(10)<<x;             //for precision upto 10 digit 
          return 0; 
          } 
          

          输入:1987 31

          输出:662.3333333333 小数点后10位

          【讨论】:

            【解决方案8】:
            #include <iostream>
            #include <iomanip>
             
            int main(void) 
            {
                float value;
                cin >> value;
                cout << setprecision(4) << value;
                return 0;
            }
            

            【讨论】:

            • 不要忘记命名空间std的使用。如果可能,请描述您的答案,仅发布代码不是一个好的答案。
            猜你喜欢
            • 1970-01-01
            • 2020-02-21
            • 2011-02-14
            • 1970-01-01
            • 2022-01-15
            • 2023-02-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多