我正在尝试以格式化文本进行一些简单的输出。Setprecision 不会将我的变量打印到小数点后两位。
例如,如果 firstItemPrice = 2.20,则输出为 2.2 而不是 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;
}