gpt4 book ai didi

c++ - 使用 GCC 时 std::put_money 不输出零值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:08 25 4
gpt4 key购买 nike

当货币值为零时,std::put_money 不会按预期输出“0”字符。我找不到任何解释此行为的内容。

例子:

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
cout.imbue(locale(""));
cout << showbase << put_money(0.0);
return 0;
}

我希望输出为“£0.00”(取决于语言环境)但我得到的是“£.00”

最终,为零值添加一个特例并不太麻烦,但这种行为对我来说仍然没有意义。这是什么原因(假设是故意的),除了单独处理零之外,是否有任何简单的解决方法?

更新

这似乎是特定于编译器的,我有报告说它在 clang 和 VS 中按预期工作,所以可能是 gcc/g++ 特定的(准确地说是 4.9 版,但其他版本似乎表现出相同的行为)

最佳答案

虽然我最初认为这看起来像 a GCC bug ,我现在认为你从 GCC 的 std::lib 得到的结果正是标准所要求的,如果你严格阅读的话。

首先,数量从 long double 转化而来使用 printf 格式说明符 "%.0Lf" 到一个窄字符串,产生“0”。这将转换为宽字符串(这对您的示例没有任何作用,因为输出是窄字符流)。

然后根据语言环境的 moneypunct<>::pos_format() 格式化输出或 moneypunct<>::neg_format()格式化模式,根据字符串是否以减号开头。

然后:

Digit characters are written, interspersed with any thousands separators and decimal point specified by the format, in the order they appear (after the optional leading minus sign)

这并没有说明添加前导零。

我认为标准未明确指定预期行为。 https://wg21.link/LWG2983会澄清的。

关于c++ - 使用 GCC 时 std::put_money 不输出零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44569834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com