C++ currency output

2019-07-17 21:03发布

问题:

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

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

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;

回答1:

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

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


回答2:

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

// 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);

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!


回答3:

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':

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

#include <cstdio>

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

Hope it helps. Good Luck!



回答4:

Check this out:

int main()
{
    double a = 133.2;

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

Output

133.20



回答5:

You can change the cout properties:

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

now cout << 133.2; will print 133.20



回答6:

You need to take a look at precision and fixed.

#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;
}