格式,iomanip,c++
我正在尝试学习使用更明确的命名空间声明,而不仅仅是说“使用命名空间 std”。我正在尝试将数据格式化为小数点后两位,并将格式设置为固定且不科学。这是我的主文件:
#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”的输出。这是有原因的吗?
另外,我认为我没有正确设置固定部分或小数点后两位,因为一旦添加了 set precision(2),我似乎就得到了科学记数法。谢谢。
I'm trying to learn to use namespaces declarations more definitive than not just say "using namespace std". I'm trying to format my data to 2 decimal places, and set the format to be fixed and not scientific. This is my main file:
#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';
}
On Visual Studio 2008, when I run my program, I get an output of "8192" before the data I want. Is there a reason for that?
Also, I don't think I am setting the fixed part or 2 decimal places correctly since I seem to get scientific notation once I added the setprecision(2). Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要
std::fixed
(另一个只是将其值插入流中,这就是您看到 8192 的原因),并且我没有看到对std::set precision
在代码中的任何位置。这将解决它:
You want
std::fixed
(the other one just inserts its value into the stream, which is why you see 8192), and I don't see a call tostd::setprecision
in your code anywhere.This'll fix it:
这可能不是您正在寻找的答案,但浮点数不适合金融计算,因为像 1/100 这样的分数无法精确表示。您最好自己进行格式化。这可以被封装:
更好(?)然而,C++ 有一个称为货币语言环境类别的工具,其中包括一个货币格式化程序 以美分作为参数。
这应该在国际上做正确的事情,打印用户的本地货币并在您的实现中隐藏小数位数。它甚至接受一美分的零头。不幸的是,这似乎不适用于我的系统(Mac OS X),该系统通常对区域设置支持很差。 (Linux 和 Windows 应该表现更好。)
It might not be the answer you're looking for, but floating-point numbers are not suited to financial calculations because fractions like 1/100 cannot be represented exactly. You might be better off doing the formatting yourself. This can be encapsulated:
Better(?) yet, C++ has a facility called the monetary locale category which includes a money formatter which takes cents as an argument.
This should Do the Right thing internationally, printing the user's local currency and hiding the number of decimal places from your implementation. It even accepts fractions of a cent. Unfortunately, this does not seem to work on my system (Mac OS X), which has generally poor locale support. (Linux and Windows should fare better.)
ios_base::fixed
不是操纵器,它是 ios 标志的值 (1 << 13
)。ios_base::fixed
is not manipulator it is a value (1 << 13
) for the ios flag.