4

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;
Jack
  • 105
  • 1
  • 2
  • 7
  • 5
    -1: The code should be a minimal working example. Most of the pasted code is irrelevant to the question. Could you please edit your question to reduce it? – thiton Mar 10 '13 at 21:18
  • Did the edit for you. Sorry I am still new to this and was unsure what part of the code was needed to help with my problem. Thanks! – Jack Mar 10 '13 at 22:11
  • 1
    Thanks. If unsure, you can orient on the phrase "short, self contained, correct example" as defined on http://sscce.org/. – thiton Mar 10 '13 at 23:32

6 Answers6

12

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!
PaperBirdMaster
  • 12,806
  • 9
  • 48
  • 94
  • Yes, the [cppreference.com docs](https://en.cppreference.com/w/cpp/io/manip/put_money) insist on this. I have seen MSVC show currency formatting with just `cout << fixed << showpoint << setprecision` if locale is set, but I believe this here solution is best for cross-platform. – Spencer Williams May 13 '23 at 02:28
5

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

cout.precision(2);
cout<< "Base Pay .................. = " << fixed << basepay << endl;
Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
  • 3
    May be worth mentioning that `fixed` is `std::fixed` from `` rather than something magical. – us2012 Mar 10 '13 at 21:19
  • Thank you! This worked great! Gave yours the answer check because you included the std::fixed so I knew how to add it! I appreciate the help! – Jack Mar 10 '13 at 21:37
  • Is there a way to use this only once so that it doesnt set the precision for the other values? – Jack Mar 10 '13 at 22:08
5

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!

1

You can change the cout properties:

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

now cout << 133.2; will print 133.20

Tom
  • 1,027
  • 2
  • 16
  • 35
1

Check this out:

int main()
{
    double a = 133.2;

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

Output

133.20

masoud
  • 55,379
  • 16
  • 141
  • 208
1

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;
}
Blake
  • 368
  • 1
  • 11
Zach Riggle
  • 2,975
  • 19
  • 26