#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
//#include <boost/format.hpp>
std::string format( double d, int place )
{
std::ostringstream ss;
ss << std::fixed << std::setprecision(place) << d ;
return ss.str() ;
}
struct format_a
{
format_a( double d, int place ) : value(d), prec(place) {}
const double value ;
const int prec ;
};
template < typename OSTREAM >
OSTREAM& operator<< ( OSTREAM& stm, const format_a& fmt )
{ return stm << std::fixed << std::setprecision(fmt.prec) << fmt.value ; }
struct format_b
{
explicit format_b( int place, int w = 0, char f = ' ' )
: prec(place), width(w), fill(f) {}
template < typename T > format_b& operator% ( const T& v )
{
std::ostringstream ss;
ss << std::fixed << std::setprecision(prec)
<< std::setw(width) << std::setfill(fill) << v ;
chars_out += ss.str() ;
return *this ;
}
const int prec ;
const int width ;
const char fill ;
std::string chars_out ;
};
template < typename OSTREAM >
OSTREAM& operator<< ( OSTREAM& stm, const format_b& fmt )
{ for( char c : fmt.chars_out ) stm << stm.widen(c) ; return stm ; }
int main ()
{
std::cout << format( .0123456789, 2 ) << '\n' ;
std::cout << format( 11.1123456789, 5 ) << '\n' ;
std::cout << format_a( 1234.2123456789, 3 ) << '\n' ;
std::wcout << format_a( 1234.3123456789, 2 ) << '\n' ;
std::cout << format_b(3) % 1234.2123456789 << '\n' ;
std::wcout << format_b( 2, 9 ) % "values:" % 999.999 % 1234.3123456789 % '\n' ;
// double x = 7.40200133400;
// std::cout << boost::format("%1$.2f")%x << '\n' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPGlvbWFuaXA+Ci8vI2luY2x1ZGUgPGJvb3N0L2Zvcm1hdC5ocHA+CgoKc3RkOjpzdHJpbmcgZm9ybWF0KCBkb3VibGUgZCwgaW50IHBsYWNlICkKewogICAgc3RkOjpvc3RyaW5nc3RyZWFtIHNzOwogICAgc3MgPDwgc3RkOjpmaXhlZCA8PCBzdGQ6OnNldHByZWNpc2lvbih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