c++ - How to make that a float use comma and not point? -


i want make operator<< use local setings or if not @ least manualy able change use of "." decimal separator ",". way of making stream (iostream, fstream, etc) , not create string , print it.

is possible?

you can imbue numpunct facet onto stream. believe should work you:

template <typename t> struct comma_separator : std::numpunct<t> {     typename std::numpunct<t>::char_type do_decimal_point() const     {         return ',';     } };  template <typename t> std::basic_ostream<t>& comma_sep(std::basic_ostream<t>& os) {     os.imbue(std::locale(std::locale(""), new comma_separator<t>));     return os; }  int main() {     std::cout << comma_sep << 3.14; // 3,14 } 

here demo.


a shorter solution, uses european locale:

std::cout.imbue(     std::locale(         std::cout.getloc(), new std::numpunct_byname<char>("de_de.utf8"))); 

but depends on locales system provides.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -