【问题标题】:Printing the correct number of decimal points with cout使用 cout 打印正确的小数位数
【发布时间】:2011-08-19 21:44:26
【问题描述】:

我有一个 float 值列表,我想用 cout 打印它们,并保留 2 个小数位。

例如:

10.900  should be printed as 10.90
1.000 should be printed as 1.00
122.345 should be printed as 122.34

我该怎么做?

setprecision 似乎对此没有帮助。)

【问题讨论】:

    标签: c++


    【解决方案1】:

    使用<iomanip>,您可以使用std::fixedstd::setprecision

    这是一个例子

    #include <iostream>
    #include <iomanip>
    
    int main()
    {
        double d = 122.345;
    
        std::cout << std::fixed;
        std::cout << std::setprecision(2);
        std::cout << d;
    }
    

    你会得到输出

    122.34
    

    【讨论】:

    • 你为什么在程序中使用“std:fixed”?
    • 可以为此定义一个有用的标头:#define FIXED_FLOAT(x) std::fixed &lt;&lt;std::setprecision(2)&lt;&lt;(x) ,这将使用简化为:cout&lt;&lt;FIXED_FLOAT(d)
    • @VilasJoshi, setprecision 设置小数点后的位数,如果有 5 位,我们使用 setprecision(2) 我们将得到 2 位,但如果有 0 位,它将显示无,使用 fixed 我们修复了必须显示的数字,因此 5 将表示为 5.00 no 5
    • @vaibnak 这误导到了错误的地步。 setprecision 设置“位数”,这取决于std::fixed 设置的状态是“有效数字”、“小数位后的数字”或“十六进制 位后的数字”
    • 感谢 Caleth 的提醒。请注意,“小数点后的 n 位”确实在固定中使用 setprecision
    【解决方案2】:

    setprecision(n) 适用于整数,而不是小数部分。您需要使用定点格式使其适用于小数部分:setiosflags(ios::fixed)

    【讨论】:

      【解决方案3】:

      你快到了,还需要使用 std::fixed,参考http://www.cplusplus.com/reference/iostream/manipulators/fixed/

      #include <iostream>
      #include <iomanip>
      
      int main(int argc, char** argv)
      {
          float testme[] = { 0.12345, 1.2345, 12.345, 123.45, 1234.5, 12345 };
      
          std::cout << std::setprecision(2) << std::fixed;
      
          for(int i = 0; i < 6; ++i)
          {
              std::cout << testme[i] << std::endl;
          }
      
          return 0;
      }
      

      输出:

      0.12
      1.23
      12.35
      123.45
      1234.50
      12345.00
      

      【讨论】:

        【解决方案4】:

        您必须将“浮动模式”设置为固定。

        float num = 15.839;
        
        // this will output 15.84
        std::cout << std::fixed << "num = " << std::setprecision(2) << num << std::endl;
        

        【讨论】:

          【解决方案5】:

          简化接受的答案

          简化示例:

          #include <iostream>
          #include <iomanip>
          
          int main()
          {
              double d = 122.345;
              std::cout << std::fixed << std::setprecision(2) << d;
          }
          

          你会得到输出

          122.34
          

          参考:

          【讨论】:

          • 这对我有用:std::cout
          【解决方案6】:

          这是一个使用矩阵的示例。

          cout<<setprecision(4)<<fixed<<m[i][j]
          

          【讨论】:

            【解决方案7】:

            只是一个小问题;将以下内容放在标题中

            使用命名空间标准;

            然后

            std::cout

            简化为

            cout

            【讨论】:

              【解决方案8】:

              要在小数点后设置固定的 2 位数字,请先使用这些:

              cout.setf(ios::fixed);
              cout.setf(ios::showpoint);
              cout.precision(2);
              

              然后打印你的双精度值。

              这是一个例子:

              #include <iostream>
              using std::cout;
              using std::ios;
              using std::endl;
              
              int main(int argc, char *argv[]) {
                  cout.setf(ios::fixed);
                  cout.setf(ios::showpoint);
                  cout.precision(2);
                  double d = 10.90;
                  cout << d << endl;
                  return 0;
              }
              

              【讨论】:

                【解决方案9】:
                #include<stdio.h>
                int main()
                
                {
                
                 double d=15.6464545347;
                
                printf("%0.2lf",d);
                
                }
                

                【讨论】:

                  【解决方案10】:

                  我在编码比赛中遇到了类似的问题,我就是这样处理的。 将所有双精度值设置为 2

                  先添加header使用setprecision

                  #include &lt;iomanip&gt;

                  然后在我们的main中添加如下代码

                    double answer=5.9999;
                    double answer2=5.0000;
                    cout<<setprecision(2)<<fixed;
                    cout <<answer << endl;
                    cout <<answer2 << endl;
                  

                  输出:

                  5.99
                  5.00
                  

                  您需要使用 fixed 来编写 5.00,这就是为什么,您的输出不会出现在 5.00。

                  A short reference video link I'm adding which is helpful

                  【讨论】:

                    【解决方案11】:

                    带有模板

                    #include <iostream>
                    
                    // d = decimal places
                    template<int d> 
                    std::ostream& fixed(std::ostream& os){
                        os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
                        os.precision(d); 
                        return os; 
                    }
                    
                    int main(){
                        double d = 122.345;
                        std::cout << fixed<2> << d;
                    }
                    

                    科学方面也类似,还有一个宽度选项(对列有用)

                    // d = decimal places
                    template<int d> 
                    std::ostream& f(std::ostream &os){
                        os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
                        os.precision(d); 
                        return os; 
                    }
                    
                    // w = width, d = decimal places
                    template<int w, int d> 
                    std::ostream& f(std::ostream &os){
                        os.setf(std::ios_base::fixed, std::ios_base::floatfield); 
                        os.precision(d); 
                        os.width(w);
                        return os; 
                    }
                    
                    // d = decimal places
                    template<int d> 
                    std::ostream& e(std::ostream &os){
                        os.setf(std::ios_base::scientific, std::ios_base::floatfield); 
                        os.precision(d); 
                        return os; 
                    }
                    
                    // w = width, d = decimal places
                    template<int w, int d> 
                    std::ostream& e(std::ostream &os){
                        os.setf(std::ios_base::scientific, std::ios_base::floatfield); 
                        os.precision(d); 
                        os.width(w);
                        return os; 
                    }
                    
                    int main(){
                        double d = 122.345;
                        std::cout << f<10,2> << d << '\n'
                            << e<10,2> << d << '\n';
                    }
                    

                    【讨论】:

                      【解决方案12】:
                      #include <iostream>
                      #include <cstdio>
                      #include <iomanip>
                      using namespace std;
                      
                      int main() {
                          int a;
                          long int b;
                          char c;
                          float d;
                          double e;
                          cin>>a>>b>>c>>d>>e;
                          cout<<a<<"\n"<<b<<"\n"<<c<<"\n";
                          cout<<fixed<<setprecision(3)<<d<<"\n";
                          cout<<fixed<<setprecision(9)<<e;
                          return 0;
                      }
                      

                      简单的代码肯定会对您有所帮助!

                      【讨论】:

                        猜你喜欢
                        • 2022-09-11
                        • 2013-12-06
                        • 1970-01-01
                        • 2018-06-25
                        • 1970-01-01
                        • 2015-12-19
                        • 1970-01-01
                        • 2021-05-15
                        相关资源
                        最近更新 更多