我有一个函数,它接受一个double并将其作为带有千分隔符的字符串返回。你可以在这里看到:c++: Format number with commas?C++:如何使用美元符号将货币格式化为货币?
#include <iomanip>
#include <locale>
template<class T>
std::string FormatWithCommas(T value)
{
std::stringstream ss;
ss.imbue(std::locale(""));
ss << std::fixed << value;
return ss.str();
}
现在,我希望能够将其与一个美元符号格式设置为货币。具体而言,如果给予20500的两倍,我想要一个字符串,例如“$ 20,500”。
因为我需要“ - $ 5,000”而不是“$ -5,000”,所以在负数的情况下, 。
使用货币,你可能会考虑['std :: put_money'](http://en.cppreference.com/w/cpp/io/manip/put_money)。 – chris
@chris - 'std :: money_put'。 –