Question

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;
}
Was it helpful?

Solution

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top