我有一個函數,它接受一個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'。 –