How to print a double with a comma

后端 未结 6 1335
醉话见心
醉话见心 2020-12-15 10:07

In C++ I\'ve got a float/double variable.

When I print this with for example cout the resulting string is period-delimited.

cout << 3.1415 <         


        
6条回答
  •  忘掉有多难
    2020-12-15 10:07

    Very old thread, but anyway ... I had the problem to fill a text entry under Gtkmm-3.0 with the outcome of a distance calculation. To make things clearer I add an example, where I have concentrated some wisdoms of several posts I read the last days:

    #include 
    // next not necessary, added only for clarity
    #include 
    Gtk::Entry m_Text;
    // a distance measured in kilometers
    double totalDistance = 35.45678;
    std::stringstream str;
    // I am using locale of Germany, pay attention to the _
    str.imbue(std::locale("de_DE"));
    // now we have decimal comma instead of point
    str << std::fixed << std::setprecision(4) << std::setw(16) << totalDistance << " km";
    // the wished formatting corresponds to "%16.4f km" in printf
    m_Text.set_text(str.str());
    

提交回复
热议问题