How to Convert Double to String C++

Save

C++ handles a variety of conversions between number types and strings using the iostream class and the put-to operator "<<." The iostream class has some limitations and doesn't support direct conversion from a double floating point number to a string. To convert from a double to a string, C++ provides the stringstream class. A simple Microsoft Visual C++ project can demonstrates this in just a few lines of code.

Things You'll Need

  • Microsoft Visual C++ 2010 Express
  • Click the Windows "Start" icon and navigate to "All Programs" and then "Microsoft Visual Studio 2010 Express." Click "Microsoft Visual C++ 2010 Express."

  • Click the "File" menu, and navigate to "New." Click "Project" to open the new project dialog.

  • Click "Win32" in the installed templates tree view on the left-hand side of the dialog. Click "Win32 Console Application." Enter a name for the project (for example, "doubletostring") in the "Name:" field toward the bottom of the dialog. Click "OK" to open the "Win32 Application Wizard" dialog. Click "Finish."

  • Replace the default code that appears in the editor with the following code:

    include "stdafx.h"
    include <iostream>
    include <string>
    include <sstream>

    using namespace std;

    int main(int argc, char* argv[])

    {

    double d;

    stringstream dss;

    string doubleasstring;

    cout &lt;&lt; "Please enter a decimal number:" &lt;&lt; endl;

    cin >> d;

    dss.precision(8);

    dss &lt;&lt; d;

    doubleasstring = dss.str();

    cout &lt;&lt; doubleasstring &lt;&lt; endl;

    return 0;

    }

  • Press "F7" or click on the "Debug" menu and "Build Solution" to compile the program and show the result in the Output window toward the bottom of Visual C++:

    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

    If the result is "0 succeeded" and then a number of failures, check the code you have entered and ensure it is identical to the code in Step 4.

  • Press "Ctrl+F5" to run the program. Enter a decimal when a black console window appears and prompts you to. This step corresponds to the following lines in the code:

    cout << "Please enter a decimal number:" << endl;

    cin >> d;

    cout << doubleasstring << endl;

    Enter code that converts from the double, stored in the variable d, to the string variable ds:

    dss.precision(8);

    dss << d;

    doubleasstring = dss.str();

    The first line sets the decimal precision of the conversion. The next line inserts the double into the stringstream dss. The final line converts the stringstream to a string.

Tips & Warnings

  • The same code works on Linux and Mac OS X using the GNU compiler collection (GCC). Create doubletostring.cpp in your favorite editor and add the code from Step 4, omitting the line: #include "stdafx.h"
  • To compile the code, use the command "g++ doubletostring.cpp."
  • To run the code, use "./a.out."

References

  • Photo Credit Jupiterimages/Comstock/Getty Images
Promoted By Zergnet

Comments

You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!