我正在尝试通过使用和编写自己的流操纵器来获取 adouble
并将其作为货币输出。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 << " ";