【发布时间】:2010-04-28 06:02:24
【问题描述】:
我正在尝试学习使用命名空间声明,而不仅仅是说“使用命名空间标准”。我正在尝试将我的数据格式化为小数点后 2 位,并将格式设置为固定且不科学。这是我的主文件:
#include <iostream>
#include <iomanip>
#include "SavingsAccount.h"
using std::cout;
using std::setprecision;
using std::ios_base;
int main()
{
SavingsAccount *saver1 = new SavingsAccount(2000.00);
SavingsAccount *saver2 = new SavingsAccount(3000.00);
SavingsAccount::modifyInterestRate(.03);
saver1->calculateMonthlyInterest();
saver2->calculateMonthlyInterest();
cout << ios_base::fixed << "saver1\n" << "monthlyInterestRate: " << saver1->getMonthlyInterest()
<< '\n' << "savingsBalance: " << saver1->getSavingsBalance() << '\n';
cout << "saver2\n" << "monthlyInterestRate: " << saver2->getMonthlyInterest()
<< '\n' << "savingsBalance: " << saver2->getSavingsBalance() << '\n';
}
在 Visual Studio 2008 上,当我运行我的程序时,我在想要的数据之前得到“8192”的输出。这有什么原因吗?
另外,我认为我没有正确设置固定部分或小数点后 2 位,因为一旦添加了 setprecision(2),我似乎得到了科学记数法。谢谢。
【问题讨论】:
-
这能回答你的问题吗? Effective use of C++ iomanip library