0

I'm trying to use a thousands separator in some serialization code and I've opted for using a custom locale for my ofstreams. A minimal example using cout (the example uses cout instead of ofstream because I have to show you something onscreen) would be the following:

#include <iostream>
#include <limits>
#include <locale>

class split_every_three : public std::numpunct<char>
{
protected:
    virtual char do_thousands_sep() const { return '\''; }
    virtual std::string do_grouping() const { return "\03"; }
};

int main()
{
    std::cout.imbue( std::locale( std::locale::classic(), new split_every_three ) );
    
    std::cout << 1234589 << std::endl;
    std::cout << 12342.424134 << std::endl;
    return 0;
}

which Outputs:

1'234'589

12'342.4

Demo

The problem is the second line, i.e. floating point numbers won't print all decimal digits. How can I fix it? (and what have I done to break this?)

Community
  • 1
  • 1
Lorah Attkins
  • 5,331
  • 3
  • 29
  • 63
  • Is the problem with the second line of output that it's not printing enough decimal places or that the decimal separator isn't what you want? – Thomas Hedden Jan 29 '17 at 01:04
  • 1
    Possibly a duplicate of http://stackoverflow.com/questions/14677448/how-to-cout-a-float-number-with-n-decimal-places – DuKes0mE Jan 29 '17 at 01:14
  • 1
    Possible duplicate of [How do I print a double value with full precision using cout?](http://stackoverflow.com/questions/554063/how-do-i-print-a-double-value-with-full-precision-using-cout) – Mike Kinghan Jan 29 '17 at 09:13

0 Answers0