为什么这个<char>C++ money_put程序不起作用?
Why is this C++ money_put<char> program not working?
我正在尝试使用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
我做错了什么?
(简化的)答案是money_put<char>.put()
函数指的是long double
的一个参数,称为units
。这是美分的单位,而不是美元。
[locale.money.put.virtuals]
参数units
被转换成一个宽字符序列,就像通过
ct.widen(buf1, buf1 + sprintf(buf1, "%.0Lf", units), buf2)
您可以在cppreference页面找到详尽的信息
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 发送cURL PUT命令,使用libcurl在c++中用程序在webhdfs中创建文件