Convert double to string using boost::lexical_cast in C++?

后端 未结 4 523
走了就别回头了
走了就别回头了 2020-12-05 19:41

I\' d like to use lexical_cast to convert a float to a string. Usually it works fine, but I have some problems with numbers without decimal. How can I fix numbe

4条回答
  •  庸人自扰
    2020-12-05 20:33

    If you need complex formatting, use std::ostringstream instead. boost::lexical_cast is meant for "simple formatting".

    std::string
    get_formatted_value(double d) {
        std::ostringstream oss;
        oss.setprecision(3);
        oss.setf(std::ostringstream::showpoint);
        oss << d;
        return oss.str();
    }
    

提交回复
热议问题