novice

Discussion in 'C++' started by Roman Töngi, Apr 2, 2005.

  1. Roman Töngi

    Roman Töngi Guest

    How can I assign a double to a string?
    I want to do something like this:

    double val = 152.36;
    string text(val);

    This does of course not work.

    Thanks
     
    Roman Töngi, Apr 2, 2005
    #1
    1. Advertising

  2. double dval = 152.36;
    string sval = atof((float)dval);

    You should include proper headers.



    Roman Töngi wrote:
    > How can I assign a double to a string?
    > I want to do something like this:
    >
    > double val = 152.36;
    > string text(val);
    >
    > This does of course not work.
    >
    > Thanks
    >
    >
     
    Gregor Razdrtic, Apr 2, 2005
    #2
    1. Advertising

  3. Roman Töngi

    Roman Töngi Guest

    thanks a lot
     
    Roman Töngi, Apr 2, 2005
    #3
  4. Roman Töngi

    Evan Guest

    Gregor's answer is incorrect; atof goes the opposite direction than
    what you need. ("'atof' : cannot convert parameter 1 from 'float' to
    'const char *'.")

    Here's the best way I know of:

    stringstream ss;
    ss << val;
    string text = ss.str();

    You'll need to include <sstream>.

    Stringstream is very useful for conversions like this. If you do it
    often, check out boost.org's lexical_cast.
     
    Evan, Apr 2, 2005
    #4
  5. Roman Töngi

    Sumit Rajan Guest

    Sumit Rajan, Apr 2, 2005
    #5
  6. Oh .... yes ... i wrote incorrect, let me fix this.
    float to std::string
    One solution follows

    float fVar = 120.5;
    char* tmpChar = new char[sizeof(float)+1];
    sprintf(tmpChar, "%f", fVar);
    std::string svar = tmpChar;
    delete [] tmpChar;

    Maybe not best , but working well

    Sumit Rajan wrote:
    > "Roman Töngi" <> wrote in message
    > news:424edaaf$...
    >
    >>How can I assign a double to a string?
    >>I want to do something like this:
    >>
    >>double val = 152.36;
    >>string text(val);
    >>
    >>This does of course not work.

    >
    >
    >
    > You may find the FAQ helpful:
    > http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.1
    >
    >



    --
    Gregor Razdrtic [Mufe]
     
    Gregor Razdrtic, Apr 2, 2005
    #6
  7. "Gregor Razdrtic" <> wrote...
    > Oh .... yes ... i wrote incorrect, let me fix this.


    First of all, plese don't top-post.

    > float to std::string
    > One solution follows
    >
    > float fVar = 120.5;
    > char* tmpChar = new char[sizeof(float)+1];


    'sizeof(float)' does not give you "how many bytes are needed for the
    decimal representation of a float". Change your fVar to 120000.5 and
    you will have undefined behaviour on your hands.

    You have to use (fabs(log10(fVar)) + 8) (or something like that) to
    determine how many chars to allocate.

    > sprintf(tmpChar, "%f", fVar);
    > std::string svar = tmpChar;
    > delete [] tmpChar;
    >
    > Maybe not best , but working well


    No, not working well. You allocate 5 chars, but print out 6 (five
    to represent the '120.5' and the sixth is the terminating 0. You
    simply write too much into 'tmpChar' than it has room.

    V

    >
    > Sumit Rajan wrote:
    >> "Roman Töngi" <> wrote in message
    >> news:424edaaf$...
    >>
    >>>How can I assign a double to a string?
    >>>I want to do something like this:
    >>>
    >>>double val = 152.36;
    >>>string text(val);
    >>>
    >>>This does of course not work.

    >>
    >>
    >>
    >> You may find the FAQ helpful:
    >> http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.1
    >>
    >>

    >
    >
    > --
    > Gregor Razdrtic [Mufe]
    >
     
    Victor Bazarov, Apr 2, 2005
    #7
  8. Roman Töngi

    codigo Guest

    "Evan" <> wrote in message
    news:...
    > Gregor's answer is incorrect; atof goes the opposite direction than
    > what you need. ("'atof' : cannot convert parameter 1 from 'float' to
    > 'const char *'.")
    >
    > Here's the best way I know of:
    >
    > stringstream ss;
    > ss << val;
    > string text = ss.str();
    >
    > You'll need to include <sstream>.
    >
    > Stringstream is very useful for conversions like this. If you do it
    > often, check out boost.org's lexical_cast.
    >


    Or template a method for any type to generate a std::string:

    #include <sstream>

    template<class T>
    std::string TtoStr(const T& r_t_)
    {
    std::eek:stringstream ossbuffer;
    ossbuffer << r_t_;
    return ossbuffer.str();
    }
     
    codigo, Apr 3, 2005
    #8
  9. Roman Töngi

    Evan Guest

    You can actually extend this idea to go from anything to anything:

    template<typename target, typename source>
    target convert(const source& s)
    {
    std::stringstream ss;
    ss << s;

    target T;
    ss >> T;
    return T;
    }

    Then use as such:
    int a = convert<int>("4");
    double b = convert<double>("4.56");
    string s = convert<string>(4);

    This is the general idea behind boost::lexical_cast
    (http://www.boost.org/libs/conversion/lexical_cast.htm), but theirs is
    much much more complicated, deals with error conditions, etc.
     
    Evan, Apr 3, 2005
    #9
  10. Roman Töngi

    codigo Guest

    "Evan" <> wrote in message
    news:...
    > You can actually extend this idea to go from anything to anything:


    nice, a universal converter.

    >
    > template<typename target, typename source>
    > target convert(const source& s)
    > {
    > std::stringstream ss;
    > ss << s;
    >
    > target T;
    > ss >> T;
    > return T;
    > }
    >
    > Then use as such:
    > int a = convert<int>("4");
    > double b = convert<double>("4.56");
    > string s = convert<string>(4);
    >
    > This is the general idea behind boost::lexical_cast
    > (http://www.boost.org/libs/conversion/lexical_cast.htm), but theirs is
    > much much more complicated, deals with error conditions, etc.
    >
     
    codigo, Apr 4, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ra

    VHDL novice question

    ra, Jul 13, 2004, in forum: VHDL
    Replies:
    6
    Views:
    2,918
    Allan Herriman
    Jul 13, 2004
  2. jbl
    Replies:
    2
    Views:
    485
    Joe Smith
    Dec 16, 2004
  3. sean
    Replies:
    1
    Views:
    620
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  4. sean
    Replies:
    2
    Views:
    2,388
  5. sean
    Replies:
    0
    Views:
    324
Loading...

Share This Page