【问题标题】:C++ currency outputC++ 货币输出
【发布时间】:2013-03-10 21:14:52
【问题描述】:

我现在正在学习 C++ 课程并完成了我的期末作业。然而,有一件事情困扰着我:

虽然我对特定输出的测试有正确的输出,但basepay 应该是133.20,它显示为133.2。有没有办法让这个显示额外的 0 而不是关闭它?

有人知道这是否可能以及如何做吗?提前谢谢你

我的代码如下:

cout<< "Base Pay .................. = " << basepay << endl;
cout<< "Hours in Overtime ......... = " << overtime_hours << endl;
cout<< "Overtime Pay Amount........ = " << overtime_extra << endl;
cout<< "Total Pay ................. = " << iIndividualSalary << endl;
cout<< endl;

cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" <<endl;
cout<< "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%" <<endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" <<endl;
cout<< "%%%% Total Employee Salaries ..... = " << iTotal_salaries <<endl;
cout<< "%%%% Total Employee Hours ........ = " << iTotal_hours <<endl;
cout<< "%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours <<endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout<< "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;

【问题讨论】:

  • -1:代码应该是一个最小的工作示例。大多数粘贴的代码与问题无关。您能否编辑您的问题以减少它?
  • 为您进行了编辑。抱歉,我对此仍然很陌生,并且不确定需要代码的哪一部分来帮助解决我的问题。谢谢!
  • 谢谢。如果不确定,您可以根据sscce.org 中定义的短语“简短、独立、正确的示例”来定位。

标签: c++ floating-point iostream currency


【解决方案1】:

如果你想用 C++ 的方式来做,并且你可以用 C++11 标志编译,你可以使用标准库:

// Note: the value in cents!
const int basepay = 10000;

// Create a stream and imbue it with the local configuration.
std::stringstream ss;
ss.imbue(std::locale(""));

// The stream contains $100.00 (assuming a en_US locale config)
ss << std::showbase << std::put_money(basepay);

例如here

这种方法有什么优势?

  • 它使用本地配置,因此输出在任何机器上都是一致的,即使是小数分隔符、千位分隔符、货币符号和小数精度(如果需要)。
  • 所有格式化工作都已由 std 库完成,工作量更少!

【讨论】:

    【解决方案2】:

    使用cout.precision 设置精度,fixed 切换定点模式:

    cout.precision(2);
    cout<< "Base Pay .................. = " << fixed << basepay << endl;
    

    【讨论】:

    • 可能值得一提的是,fixed 是来自&lt;iostream&gt;std::fixed,而不是神奇的东西。
    • 谢谢!这很好用!给你的答案检查,因为你包括 std::fixed 所以我知道如何添加它!感谢您的帮助!
    • 有没有办法只使用一次这样它就不会设置其他值的精度?
    【解决方案3】:

    是的,这可以使用stream manipulators 来实现。例如,将输出设置为固定浮点表示法,定义精度(在您的情况下为 2)并将填充字符定义为“0”:

    #include <iostream>
    #include <iomanip>
    
    int main()
    {
        double px = 133.20;
        std::cout << "Price: "
                  << std::fixed << std::setprecision(2) << std::setfill('0')
                  << px << std::endl;
    }
    

    如果您更喜欢 C 风格的格式,这里有一个使用 printf() 来实现相同效果的示例:

    #include <cstdio>
    
    int main()
    {
        double px = 133.20;
        std::printf("Price: %.02f\n", px);
    }
    

    希望对您有所帮助。祝你好运!

    【讨论】:

      【解决方案4】:

      看看这个:

      int main()
      {
          double a = 133.2;
      
          cout << fixed << setprecision(2) << a << endl;
      }
      

      输出

      133.20

      【讨论】:

        【解决方案5】:

        您可以更改cout 属性:

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

        现在cout &lt;&lt; 133.2; 将打印133.20

        【讨论】:

          【解决方案6】:

          你需要看看precisionfixed

          #include <iostream>
          
          int main()
          {
              double f = 133.20;
          
              // default
              std::cout << f << std::endl;
          
              // precision and fixed-point specified
              std::cout.precision(2);
              std::cout << std::fixed << f << std::endl;
          
              return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-08-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-09-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多