How to Convert Double to String C++

Written by gary sims
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Convert Double to String C++
Use Visual C++ to write a C++ program to convert a double to a string. (Jupiterimages/Comstock/Getty Images)

C++ handles a variety of conversions between number types and strings using the iostream class and the putto 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.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Microsoft Visual C++ 2010 Express

Show MoreHide

Instructions

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

    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 << "Please enter a decimal number:" << endl;

    cin >> d;

    dss.precision(8);

    dss << d;

    doubleasstring = dss.str();

    cout << doubleasstring << endl;

    return 0;

    }

  5. 5

    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.

  6. 6

    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 and warnings

  • The same code works on Linux and Mac OS X using the GNU compiler collection (GCC). Create doubletostring.cpp in your favourite 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."

Don't Miss

References

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.