格式,iomanip,c++

发布于 2024-08-30 12:34:54 字数 1107 浏览 2 评论 0原文

我正在尝试学习使用更明确的命名空间声明,而不仅仅是说“使用命名空间 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

影子是时光的心 2024-09-06 12:34:54

您想要 std::fixed (另一个只是将其值插入流中,这就是您看到 8192 的原因),并且我没有看到对 std::set precision 在代码中的任何位置。
这将解决它:

#include <iostream>
#include <iomanip>

using std::cout;
using std::setprecision;
using std::fixed;

int main()
{
    cout << fixed << setprecision(2)
         << "saver1\n" 
         << "monthlyInterestRate: " << 5.5 << '\n' 
         << "savingsBalance: " << 10928.8383 << '\n';
    cout << "saver2\n" 
         << "monthlyInterestRate: " << 4.7 << '\n' 
         << "savingsBalance: " << 22.44232 << '\n';
}

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 to std::setprecision in your code anywhere.
This'll fix it:

#include <iostream>
#include <iomanip>

using std::cout;
using std::setprecision;
using std::fixed;

int main()
{
    cout << fixed << setprecision(2)
         << "saver1\n" 
         << "monthlyInterestRate: " << 5.5 << '\n' 
         << "savingsBalance: " << 10928.8383 << '\n';
    cout << "saver2\n" 
         << "monthlyInterestRate: " << 4.7 << '\n' 
         << "savingsBalance: " << 22.44232 << '\n';
}
要走干脆点 2024-09-06 12:34:54

这可能不是您正在寻找的答案,但浮点数不适合金融计算,因为像 1/100 这样的分数无法精确表示。您最好自己进行格式化。这可以被封装:

class money {
    int cents;
public:
    money( int in_cents ) : cents( in_cents ) {}

    friend ostream &operator<< ( ostream &os, money const &rhs )
        { return os << '

更好(?)然而,C++ 有一个称为货币语言环境类别的工具,其中包括一个货币格式化程序 以美分作为参数。

locale::global( locale("") );
use_facet< money_put<char> >( locale() ).put( cout, false, cout, ' ', 123 );

这应该在国际上做正确的事情,打印用户的本地货币并在您的实现中隐藏小数位数。它甚至接受一美分的零头。不幸的是,这似乎不适用于我的系统(Mac OS X),该系统通常对区域设置支持很差。 (Linux 和 Windows 应该表现更好。)

<< m.cents / 100 << '.' << m.cents % 100; } }; cout << money( 123 ) << endl; // prints $1.23

更好(?)然而,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:

class money {
    int cents;
public:
    money( int in_cents ) : cents( in_cents ) {}

    friend ostream &operator<< ( ostream &os, money const &rhs )
        { return os << '

Better(?) yet, C++ has a facility called the monetary locale category which includes a money formatter which takes cents as an argument.

locale::global( locale("") );
use_facet< money_put<char> >( locale() ).put( cout, false, cout, ' ', 123 );

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.)

<< m.cents / 100 << '.' << m.cents % 100; } }; cout << money( 123 ) << endl; // prints $1.23

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.)

近箐 2024-09-06 12:34:54
cout << setiosflags(ios::fixed) << setprecision(2) << 1/3.;

ios_base::fixed 不是操纵器,它是 ios 标志的值 (1 << 13)。

cout << setiosflags(ios::fixed) << setprecision(2) << 1/3.;

ios_base::fixed is not manipulator it is a value (1 << 13) for the ios flag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文