【问题标题】:Why is this C++ money_put<char> program not working?为什么这个 C++ money_put<char> 程序不工作?
【发布时间】:2014-07-01 04:03:48
【问题描述】:

我正在尝试使用 C++ 语言环境,但无法弄清楚为什么输出是 0.05 而不是 4.98

#include <iostream>
#include <vector>
#include <string>
#include <locale>
using namespace std;

int main(int argc, const char** argv) {


    vector<string> locales;
    locales.push_back("de_DE");
    locales.push_back("en_AU");
    locales.push_back("en_GB");
    locales.push_back("zh_CN");

    long double amount = 4.98;
    for (size_t i = 0, s = locales.size(); i < s; ++i) {
        if (locales[i] != "C") {
            cout.imbue(locale(locales[i].c_str()));
            cout << i << " (" << locales[i] << "): ";

            const moneypunct<char>& mp = use_facet<moneypunct<char> >(cout.getloc());
            const money_put<char>& mv = use_facet<money_put<char> >(cout.getloc());

            cout << mp.curr_symbol();
            ostreambuf_iterator<char> out(cout);
            mv.put(out, false, cout, cout.fill(), amount);
            cout << endl;
        }
    }

    return 0;
}

程序的输出如下:

0 (de_DE): Eu0,05
1 (en_AU): $0.05
2 (en_GB): £0.05
3 (zh_CN): ¥0.05

我做错了什么?

【问题讨论】:

  • 因为 amount 设置为 4.98。

标签: c++ localization internationalization locale


【解决方案1】:

(简化的)答案是money_put&lt;char&gt;.put() 函数引用了一个名为unitslong double 参数。这是 cents 的单位,而不是 dollars

【讨论】:

    【解决方案2】:

    [locale.money.put.virtuals]

    参数units 被转换成一个宽字符序列,好像是由

    ct.widen(buf1, buf1 + sprintf(buf1, "%.0Lf", units), buf2)
    

    您可以在cppreference 页面上找到详尽的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 2016-04-20
      • 2018-03-13
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多