How to format monetary values with C++?

Asked

Viewed 1,760 times

4

I would like to present values formatted as currency, with thousands and cents separators.

I would like for example, 56000/12 present 4.666,67. I can present 4,666.67. Would there be some way to change the . (point) by , (comma)?

If you want, this is the code I use (the first 11 entries are 5000 and the 12th is 1000):

#include <iostream>
#include <iomanip>
#include <locale.h>
using namespace std;

class BRL : public numpunct<char>
{
    protected:

    virtual char do_thousands_sep() const
    {
        return ',';
    }

    virtual std::string do_grouping() const
    {
        return "\03";
    }
};

int menor5000;
float stop,media,mes,tot,var;
int main(int argc, char* argv[])
{
    setlocale(LC_ALL, "portuguese");

    cout <<"\n\t\tExercício 5\n";
    locale br(locale(), new BRL());
    cout.imbue(br);

    for (menor5000,mes=1,stop=0;stop<12;stop++,mes++)
    {
        cout<<"Digite o valor da venda no " << mes <<"º mês: ";
        cin>>var;
        if (var < 5000)
        menor5000++;
        tot = tot + var;
    }

media = tot / 12;
cout<<"\n\nA media de vendas mensais em 2013 é de R$ " << setiosflags (ios::fixed) << setprecision(2) << media << "\n";
cout << "\nForam realizadas " << menor5000 << " vendas menores que R$ 5,000.00.";

return 0;
system ("pause");
}
  • 1

    Does that answer you? http://answall.com/questions/9810/formatr-decimal-com-virgula-e-mile_com-dot

2 answers

5

Now, you practically have the answer yourself. Just modify the class you inherit from numpunct<char>. Implement the do_decimal_point to return a comma and do the do_thousands_sep return a point. Here is an example:

#include <locale.h>
#include <iostream>
#include <iomanip>

using namespace std;

class CurrencyLocale : public numpunct<char> {
    virtual char do_thousands_sep() const   { return '.'; }
    virtual char do_decimal_point() const   { return ','; }
    virtual std::string do_grouping() const { return "\03"; }
};

int main() {
    locale currency(locale(), new CurrencyLocale());
    cout.imbue(currency);

    cout << fixed << setprecision(2) << 5654214.54 << endl;
}

Result is:

5.654.214,54

1

There is a method called Replace(), you can work it together with the Find(). Here is a short example of how to use:

I switched to answer Maniero

#include <iostream>
#include <string>

int main ()
{
  using namespace std;

  string str;

  str = "5,000.00";

  str.replace(str.find(','),1,"@");
  str.replace(str.find('.'),1,",");
  str.replace(str.find('@'),1,".");

  cout << ("%s",str);

  return 0;
}
  • Okay, I saw how you change the dot to the comma. And how you’re going to change comma to the dot?

  • [Edit]and its answer to put the example.

  • Is in the answer the example.

  • You don’t need the credit. Because you changed the answer not to be wrong. The way it was, it resulted in "5,000,00".

  • I took the question as a basis: i can present 4,666.67 would have some way to exchange the . (point) by , (comma)?. Thanks for the remark @bigown

  • 1

    Yes, the question was poorly asked, it is normal when the OP does not understand exactly the problem he is trying to solve, then we have to try to supply this deficiency.

  • -1. This will not work with values that have more than one decimal separator, such as 1,000,000.00. The solution is to manipulate the locale to generate the value already in the right format, instead of changing it later. Also, what do you think cout << ("%s",str); does? The literal before the comma is completely ignored.

  • @Guilhermebernal If the solution is to manipulate the locale, the question is duplicated that the bigown linked up there?

  • @bfavaretto I hadn’t read his comment. Well... he seems to want the American format and not the Brazilian as in the question, but the idea is exactly the same, changing only some parameters as I mentioned in the answer. I think you can consider duplicate yes.

  • @Guilhermebernal Or maybe not, because with Qt the most voted answer there seems simpler (do not know C++ to judge right). And here the question is no Qt.

  • @Guilhermebernal had still forgotten this detail. I no longer liked the solution :)

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.