#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11

    double to string C++


    Hello,
    Okay, I have another question for yall. I am having a little problem going from double to string.

    I am currently saving to a text file then reading it back, but I don't like this away at all. Could you show me an easier way please.

    Okay thanks for the help.
    ~Ronald



    No replies found.
    Last edited by calculus87; September 18th, 2003 at 12:51 PM.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Huh? I interpret you as saying that you want to take the value of a double variable and assign the string representation of that value to a string.

    Why not use sprintf?

    Or if you insist on a purely C++ solution, then use a string stream:

    Code:
    #include <sstream>
    
    ...
    
    ostringstream buf;
    double dNum;
    string  sStr;
    
    buf << dNum;
    sStr = buf.str();   // .str() returns string object associated with buf
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11

    Answer to double to string


    Okay, that works wonderfully. Thanks alot

    #include <iostream>
    using namespace std;
    #include <string>
    #include <sstream>

    void main()
    {

    stringstream stream;
    double dub=-12.3;

    stream << dub; // shove it into the stream

    string dubble = stream.str(); // create a string of dub
    cout<<dub<<" "<<dubble<<endl;
    }
    This message is a natural product made from recycled electrons. The slight
    variations in spelling and grammar enhance its individual character and
    beauty and in no way are to be considered flaws or defects.

IMN logo majestic logo threadwatch logo seochat tools logo