1

I am inserting some ints in my ostream but big numbers or even years, like 3000 or 25000123 are being formatted like this 3.000 or 25.000.123.

I am not sure why this is happening. It might be because I usedd imbue("") on the stream so decimal numbers were shown like this 14,53 instead of 14.53 but I commented that line and everything stays the way it was.

I just want to get rid of those dots when getting numbers in the outstream (but I don't want to get rid of the decimal comma either). How can I do this?

I thought maybe the iomanip library would help but I didn't find anything regarding this situation.

std::ostream& operator <<(std::ostream& os, const Article& a) {
    os << a.title() << a.pub_date().year() << ". " << a.price() << "";
    return os;
}
dabadaba
  • 9,064
  • 21
  • 85
  • 155

1 Answers1

1

You can continue using imbue if you specify a custom grouping for numbers.

See an example here: http://www.cplusplus.com/reference/locale/numpunct/grouping/

Taking that example we have this code which verifies that no grouping is done even though the locale is set.

// numpunct::grouping example
#include <iostream>       // std::cout
#include <string>         // std::string
#include <locale>         // std::locale, std::numpunct, std::use_facet

// custom numpunct with grouping:
struct my_numpunct : std::numpunct<char> {
    // the zero by itself means do no grouping
    std::string do_grouping() const {return "\0";}
};

int main() {
    std::locale loc (std::cout.getloc(),new my_numpunct);
    std::cout.imbue(loc);
    std::cout << "one million: " << 1000000 << '\n';
    return 0;
}
Sean Perry
  • 3,776
  • 1
  • 19
  • 31
  • 1
    Ahh, you just beat me to it. You may as well add the demical stuff that the OP likes, just to make sure `char do_decimal_point() const { return ','; }` as it isn't really worth a separate answer. – Rook May 14 '14 at 18:05
  • His locale was probably already doing the right thing for decimals, that is why he used ``imbue`` in the first place. But yeah, the explicit use commas would be a good idea. – Sean Perry May 14 '14 at 18:09
  • Its a potentially useful or interesting thing for anyone happening on this question in the future. But certainly not important. – Rook May 14 '14 at 18:14