10

I'm trying to do some simple output in formatted text. Setprecision is not printing my variables out to two decimal places.

For example if firstItemPrice = 2.20, the output is 2.2 instead of 2.20

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

    string firstitem = "";
    string seconditem = "";
    double firstItemNum;    
    double firstItemPrice = 0.00;
    double secondItemNum;
    double secondItemPrice = 0.00;

    //first item
    cout << "Enter the name of Item 1: ";
    getline(cin, firstitem);
    cout << "Enter the number of " << firstitem << "s and the price of each: ";
    cin >> firstItemNum >> firstItemPrice;
    cin.ignore();

    //second item
    cout << "Enter the name of Item 2: ";
    getline(cin, seconditem);
    cout << "Enter the number of " << seconditem << "s and the price of each: ";
    cin >> secondItemNum >> secondItemPrice;


    cout << left << setw(20) << "Item"  << setw(10) << "Count"
    << setw(10) << "Price" << left << "\n";

    cout << setw(20) << "====" << setw(10) << "====" << setw(10)
    << "====" << left << "\n";

    cout << setw(20) << firstitem << setw(10)
    << firstItemNum << setw(10) << setprecision(2)
    << firstItemPrice << "\n";

    cout << setw(20) << seconditem << setw(10) << secondItemNum
    << setprecision(2) << secondItemPrice << left << "\n";


    return 0;
}
lloyd
  • 1,089
  • 1
  • 17
  • 39
  • Your code is fine. I compiled it. It works fine. It prints two decimals. You might want to try a different compiler. –  Mar 12 '14 at 02:12

2 Answers2

12

You need a fixed in there to do that.

cout << fixed;

Set it back using:

cout.unsetf(ios_base::floatfield);

In your case, changing the last bit of your program like this example should do it:

cout << setw(20) << firstitem << setw(10)
<< firstItemNum << setw(10) << fixed << setprecision(2)
<< firstItemPrice << "\n";

cout.unsetf(ios_base::floatfield);

cout << setw(20) << seconditem << setw(10) << secondItemNum
<< fixed << setprecision(2) << secondItemPrice << left << "\n";

Editorial aside: Don't use floating point numbers to represent currency values.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • to find out why float are bad for financial computation check out: http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency and http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic#Libraries – fduff Feb 13 '14 at 13:33
6

from http://www.cplusplus.com/reference/ios/ios_base/precision/

The floating-point precision determines the maximum number of digits to be written on insertion operations to express floating-point values. How this is interpreted depends on whether the floatfield format flag is set to a specific notation (either fixed or scientific) or it is unset (using the default notation, which is not necessarily equivalent to either fixed nor scientific).

For the default locale: Using the default floating-point notation, the precision field specifies the maximum number of meaningful digits to display in total counting both those before and those after the decimal point. Notice that it is not a minimum, and therefore it does not pad the displayed number with trailing zeros if the number can be displayed with less digits than the precision. In both the fixed and scientific notations, the precision field specifies exactly how many digits to display after the decimal point, even if this includes trailing decimal zeros. The digits before the decimal point are not relevant for the precision in this case.

Community
  • 1
  • 1
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158