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

后端 未结 4 522
走了就别回头了
走了就别回头了 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:10

    Have a look at boost::format library. It merges the usability of printf with type safety of streams. For speed, I do not know, but I doubt it really matters nowadays.

    #include 
    #include 
    
    int main()
    {
       double x = 5.0;
       std::cout << boost::str(boost::format("%.2f") % x) << '\n';
    }
    

提交回复
热议问题