我正在尝试获取一个 double
并通过使用 iomanip
并编写我自己的流操纵器将其输出为货币。为此,我编写了一个方法,该方法返回一个 ostream 并将一个 ostream 作为参数(以及两个整数参数,宽度和精度)
std::ostream& Currency(std::ostream& os, int width, int precision)
{
os << "$";
os << std::setprecision(precision);
os << std::right;
os << std::setw(width);
os << std::fixed << std::setfill('0');
return os;
}
但是,我得到的不是我期望的输出 $0000128.00
,而是输出 $0x7ffeb45783d8128.00
。
我只是这样调用它:
cout << Currency(cout, 11, 2) << balance << endl;
不知道是什么问题,好像是被包裹在一个函数里了。如果我将不在函数中的确切代码复制到我的实际输出中,那么它就可以完美运行。
此外,在我对余额运行我的货币操纵器之前,我还运行:
out << std::right << std::setfill('0') << std::setw(10) << a.accountNo << " ";
out << std::setfill(' ') << std::left << std::setw(19) << a.name << " ";
out << std::setfill(' ') << std::left << std::setw(3) << a.sex << " ";
out << std::setfill(' ') << std::left << std::setw(10) << a.dob.toString() << " ";
out << std::setfill(' ') << std::left << std::setw(40) << a.address << " ";
最佳答案
cout << Currency(cout, 11, 2) << balance << endl;
Currency
函数执行后,您会得到类似于以下内容的结果:
cout << cout << balance << endl;
没有用于打印 ostream
的重载,但是有一个到 void*
的转换,旨在用于启动并生成打印输出的 bool 上下文得到
关于c++ - iomanip 操作返回一个十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30454365/