Boost logo

Boost Users :

Subject: [Boost-users] [boost.multiprecision] print cpp_int with a thousands separator
From: Tassilo Glander (tgl_at_[hidden])
Date: 2016-05-03 09:26:33


Dear List,

how can I control the thousands separator behavior when streaming a cpp_int to std::cout?

I think it is not possible without changing the global locale, as cpp_int.str uses lexical_cast, which uses the global locale object to read the thousands separator character. This prevents me from having a custom formatting like below.

Is there another way?

Best,
Tassilo

#include <iostream>
#include <locale>
#include <iomanip>

int main()
{
    struct my_numpunct : std::numpunct<char>
    {
        protected :
           virtual char do_thousands_sep() const override { return '.' ; }
           virtual std::string do_grouping() const override { return "\03" ; }
    };

   cpp_int testNum(1000);
    std::cout.imbue( std::locale( std::cout.getloc(), new my_numpunct ) ) ;
    std::cout << testNum << '\n' ; // still get 1000, desired is 1.000
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net