C++货币输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15327706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:16:07  来源:igfitidea点击:

C++ currency output

c++floating-pointiostreamcurrency

提问by Hyman

I am taking a C++ course right now and have completed my final assignment. However there is one thing that is bugging me:

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

Though I have the correct outputs for the testing on a particular output, the basepayshould be 133.20and it is displaying as 133.2. Is there a way to have this display the extra 0 rather than leaving it off?

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

Anyone know if it's possible and how to do it? Thank you in advance

任何人都知道是否有可能以及如何做到这一点?先感谢您

My code is below:

我的代码如下:

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;

采纳答案by Mike Dinescu

use cout.precisionto set precision, and fixedto toggle fixed-point mode:

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

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

回答by PaperBirdMaster

If you want to do it in a C++ way, andyou can compile with C++11 flags, you can use the standard library:

如果您想以 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 0.00 (assuming a en_US locale config)
ss << std::showbase << std::put_money(basepay);

Example here.

示例在这里

What advantages have this approach?

这种方法有什么优点?

  • It uses the local configuration, so the output will be coherent in any machine, even for the decimal separator, thousand separator, money symbol and decimal precission (if needed).
  • All the format effort is already done by the std library, less work to do!
  • 它使用本地配置,因此输出在任何机器上都是一致的,即使是小数分隔符、千位分隔符、货币符号和小数精度(如果需要)。
  • 所有格式化工作都已由 std 库完成,要做的工作更少!

回答by PaperBirdMaster

Yes, this is possible to do using stream manipulators. For example, set output to fixed floating-point notation, define precision (2 in your case) and define the fill character to '0':

是的,这可以使用流操纵器来完成。例如,将输出设置为固定浮点表示法,定义精度(在您的情况下为 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;
}

In case you prefer a C-style formatting, here is an example of using printf()to achieve the same:

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

#include <cstdio>

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

Hope it helps. Good Luck!

希望能帮助到你。祝你好运!

回答by Tom

You can change the coutproperties:

您可以更改cout属性:

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

now cout << 133.2;will print 133.20

现在cout << 133.2;将打印133.20

回答by masoud

Check this out:

看一下这个:

int main()
{
    double a = 133.2;

    cout << fixed << setprecision(2) << a << endl;
}

Output

输出

133.20

133.20

回答by Zach Riggle

You need to take a look at precisionand fixed.

你需要看看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;
}