我試圖學習使用命名空間聲明比不只是說「使用命名空間標準」。我試圖將我的數據格式化爲2位小數,並將格式設置爲固定而不科學。這是我的主要文件:格式,iomanip,C++
#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)後得到了科學記數法。謝謝。
'money_put' variant在我的機器上打印'123'而不是'$ 1.23'。它在任何語言環境中都不應該是可接受的輸出。 – jfs 2010-04-28 09:44:43
@ J.F。 - 預期產出是$ 1.23'。你在使用什麼平臺? – Potatoswatter 2010-04-28 16:15:34
@Patatoswatter:http://codepad.org/EY5PqSIw – jfs 2010-04-29 08:35:09