Converting Double to String in C++

前端 未结 3 1868
迷失自我
迷失自我 2020-12-31 12:00

I am having some issues trying to convert a double to C++ string. Here is my code

std::string doubleToString(double val)
{
    std::ostringstream out;
    ou         


        
相关标签:
3条回答
  • 2020-12-31 12:47

    You can also set the minimum width and fill char with STL IO manipulators, like:

    out.width( 9 );
    out.fill( ' ' );
    
    0 讨论(0)
  • 2020-12-31 12:53
    #include <iomanip>
    
    std::string doubleToString(double val)
    {
       std::ostringstream out;
       out << std::fixed << val;
       return out.str();
    }
    
    0 讨论(0)
  • 2020-12-31 12:55
    #include <iomanip>
    using namespace std;
    // ...
    out << fixed << val;
    // ...
    

    You might also consider using setprecision to set the number of decimal digits:

    out << fixed << setprecision(2) << val;
    
    0 讨论(0)
提交回复
热议问题