converting double to string??

This is a discussion on converting double to string?? within the C++ Programming forums, part of the General Programming Boards category; how do you convert from a double to a string in c++.. i tried this...but i dont think its right ...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    converting double to string??

    how do you convert from a double to a string in c++..

    i tried this...but i dont think its right (cause it doesnt work)

    NOTE:currentTime is the new string and time is the double

    /* convert double to string */
    std::strstream strm(currentTime,time,0);
    strm >> currentTime;

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    I think <strstream> is deprecated. Use <sstream>.

    Code:
    string DoubleToString( double d )
    {
      stringstream ss;
      ss << d;
      return ss.str();
    }

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Or use boost

    Code:
    #include <boost/lexical_cast.hpp>
    
    double MyDouble = 1.602;
    std::string MyStr = boost::lexical_cast<string> (MyDouble);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 01:04 PM
  2. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 03:56 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. converting string to double
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21