【问题标题】:format, iomanip, c++格式,iomanip,C++
【发布时间】: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),我似乎得到了科学记数法。谢谢。

【问题讨论】:

标签: c++ iomanip


【解决方案1】:

您想要std::fixed(另一个只是将其值插入到流中,这就是您看到 8192 的原因),而我在您的代码中的任何地方都没有看到对 std::setprecision 的调用。
这将解决它:

#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';
}

【讨论】:

    【解决方案2】:

    这可能不是您要寻找的答案,但浮点数不适合财务计算,因为无法准确表示 1/100 之类的分数。您最好自己进行格式化。这可以封装:

    class money {
        int cents;
    public:
        money( int in_cents ) : cents( in_cents ) {}
    
        friend ostream &operator<< ( ostream &os, money const &rhs )
            { return os << '$' << m.cents / 100 << '.' << m.cents % 100; }
    };
    
    cout << money( 123 ) << endl; // prints $1.23
    

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

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

    这应该在国际范围内做正确的事情,打印用户的本地货币并在您的实施中隐藏小数位数。它甚至接受一分钱。不幸的是,这似乎不适用于我的系统(Mac OS X),它的语言环境支持通常很差。 (Linux 和 Windows 应该会更好。)

    【讨论】:

    • money_put 变体在我的机器上打印 123 而不是 $1.23。它不应该是任何语言环境下可接受的输出。
    • @J.F. - 预期输出为$1.23。你用的是什么平台?
    • @J.F.这很不幸,但这仅意味着您的平台已损坏。 (例如,您的 LANG 环境变量未设置。)随便。
    • @Potatoswatter:你的意思是我的codepad.org的平台坏了。 codepad.org 在服务器端编译/运行代码。顺便说一句,LANG 在我测试过的机器上是 en_US.UTF-8
    • @J.F. codepad.org/Adxmw6fC CodePad 肯定没有设置LANG,而且作为一个国际站点,他们的本地货币应该是什么?对此的支持很差,但标准非常清楚输出应该是什么样子。
    【解决方案3】:
    cout << setiosflags(ios::fixed) << setprecision(2) << 1/3.;
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多