1
Win7-64
Win10-64
VC++ 16.9.4

I'm trying to use put_money to:

  • insert a monetary symbol ($)
  • insert locale separators for thousands/decimal point

In particular, for locale("en-US") if:

    long double x = 1234567891;
    cout << put_money(x) << endl;

then I would like to see something like $123,456.79 as output and the examples I've looked at seem to agree. Instead I see 123456.

My use of locale is:

 # include <fstream>
 # include <sstream>
 # include <string>

ofstream repout;
void openFile(string filename) {
   if (filename.empty()) {
      cout << "Filename missing" << endl;
   } else {
      try {
         repout.open(filename);
         locale mylocale("");
         repout.rdbuf()->pubimbue(mylocale);
      } catch (exception& e) {
         stringstream str;
         str << "Unable to open: " << filename << ": " << e.what();
         cout << str.str() << endl;
      }
   }
}; // int openFile(string filename) 
lostbits
  • 928
  • 1
  • 9
  • 23
  • It could depend on the OS, and many other settings. On Linux see [locale(7)](https://man7.org/linux/man-pages/man7/locale.7.html). – Basile Starynkevitch May 12 '21 at 18:19
  • Can you show a [example] where you set the locale? – JohnFilleau May 12 '21 at 18:21
  • See [this](https://stackoverflow.com/a/27615711/2027196) for an example of printing out all available locales in your implementation. – JohnFilleau May 12 '21 at 18:23
  • Like your title... A question asking how to put money in venture capital :-P – einpoklum May 12 '21 at 18:28
  • @JohnFilleau Example below (I hope its readable) – lostbits May 12 '21 at 19:08
  • @JohnFilleau void Report_Commands::openFile(string filename) { if (filename.empty()) { Log::Error("Filename missing"); } else { try { repout.open(filename); locale mylocale(""); repout.rdbuf()->pubimbue(mylocale); } catch (exception& e) { stringstream str; str << "Unable to open: " << filename << ": " << e.what(); Log::Error(str.str()); useLogFile(); } } }; // int Report_Commands::openFile(string filename) – lostbits May 12 '21 at 19:08
  • @einpoklum Now That's funny. Thanks for pointing it out! – lostbits May 12 '21 at 19:10
  • @arthur you're able to edit your question. Please add relevant code in there. – JohnFilleau May 12 '21 at 20:01
  • @JohnFilleau Thanks for the tip. Question modified to show code used. The websites I've looked at show that locale mylocale("") should default to the regional locale, in my case, the US. I thought that this would cause put_money() to output a '$' for money, commas for thousands separators, and a decimal point for currency fractions. What I get is no dollar sign ('$'), no thousands separator and no currency fractions. I have tried, as per suggestions, to use locale("en-US") to no effect. So I'm stumped. – lostbits May 12 '21 at 21:13
  • @JohnFilleau [this](https://stackoverflow.com/questions/27614666/print-all-stdlocale-names-windows/27615711#27615711) has non-compilable code, even with using #include . The arguments for EnumSystemLocalesEx in the code appear correct. – lostbits May 12 '21 at 21:20

1 Answers1

0

Instead of waiting for a solution, I decided to write my own little piece of code. Seems to work.

# include <sstream>
# include <string>

string putmoney(double x) {
   static const string money = "$";                      //!< monetary symbol
   static const string thou  = ",";                      //!< thousands symbol
   stringstream out;
   out << setprecision(2) << fixed << x;                 //  create money

   string tmp = out.str()                                //!< copy money into temporary string
        , str = money;                                   //!< putmoney output string

   int size = tmp.size() - ((x < 0) ? 4 : 3)             //!< size w/o fraction, minus sign and decimal point
     , th   = (size - 1 )/ 3                             //!< number of thousands
     , rem  = size % 3;                                  //!< number of excess char

   if (th == 0) return str + tmp;                        //   0.0 < number < 999.99

   rem  = (rem == 0) ? 3 : rem;                          //   move the three leading characters
   rem += (x < 0) ? 1 : 0;                               //   add one if the number is negative
   str += tmp.substr(0, rem);                            //   produces $# or $-#

   for (; th > 0; th--) {                                //   insert thousands seperators
      str += thou + tmp.substr(rem, 3);
      rem += 3;
   }
   str += tmp.substr(tmp.size() - 3, 3);                 //   move fractional part
   return str;
}; // string putmoney(double x)
lostbits
  • 928
  • 1
  • 9
  • 23