Conversion from double to string (from newbie)

Discussion in 'C++' started by Marco Traverso, Dec 6, 2003.

  1. Hi all,
    This may seem quite a basic question...
    Is there a direct function to convert a double into a string? I'm used to
    VBScript and it has the CStr function that does the job.

    es. (vbScript)

    Dim dN
    Dim sString

    dN =- Sqr(2)
    sString = CStr(dN)

    'sString returns "-1.4142135623731"

    I've searched for a C / C++ function, and I found only the _ecvt
    function (and other similar...).

    Anyway they have slightly different input variables so the conversion is not
    "automatic"

    Is there a specific function or do I have to build my own?

    Thank you in advance,

    Marco
     
    Marco Traverso, Dec 6, 2003
    #1
    1. Advertising

  2. Marco Traverso

    lallous Guest

    "Marco Traverso" <> wrote in message
    news:mwnAb.9931$...
    > Hi all,
    > This may seem quite a basic question...
    > Is there a direct function to convert a double into a string? I'm used to
    > VBScript and it has the CStr function that does the job.
    >
    > es. (vbScript)
    >
    > Dim dN
    > Dim sString
    >
    > dN =- Sqr(2)
    > sString = CStr(dN)
    >
    > 'sString returns "-1.4142135623731"
    >
    > I've searched for a C / C++ function, and I found only the _ecvt
    > function (and other similar...).
    >
    > Anyway they have slightly different input variables so the conversion is

    not
    > "automatic"
    >
    > Is there a specific function or do I have to build my own?
    >
    > Thank you in advance,
    >
    > Marco
    >
    >
    >

    Hello,

    Checkout the sprintf() function.

    --
    Elias
     
    lallous, Dec 6, 2003
    #2
    1. Advertising

  3. Thank you Elias!

    Marco

    "lallous" <> ha scritto nel messaggio
    news:bqt2n2$uaf37$-berlin.de...
    > "Marco Traverso" <> wrote in message
    > news:mwnAb.9931$...
    > > Hi all,
    > > This may seem quite a basic question...
    > > Is there a direct function to convert a double into a string? I'm used

    to
    > > VBScript and it has the CStr function that does the job.
    > >
    > > es. (vbScript)
    > >
    > > Dim dN
    > > Dim sString
    > >
    > > dN =- Sqr(2)
    > > sString = CStr(dN)
    > >
    > > 'sString returns "-1.4142135623731"
    > >
    > > I've searched for a C / C++ function, and I found only the _ecvt
    > > function (and other similar...).
    > >
    > > Anyway they have slightly different input variables so the conversion is

    > not
    > > "automatic"
    > >
    > > Is there a specific function or do I have to build my own?
    > >
    > > Thank you in advance,
    > >
    > > Marco
    > >
    > >
    > >

    > Hello,
    >
    > Checkout the sprintf() function.
    >
    > --
    > Elias
    >
    >
     
    Marco Traverso, Dec 6, 2003
    #3
  4. > Is there a direct function to convert a double into a string?
    You can use string streams to do so:

    #include <sstream>

    std::eek:stringstream ostr;

    double d = 123.456;
    ostr << d;
    cout << ostr.str();


    Thierry
     
    Thierry Miceli, Dec 6, 2003
    #4
  5. Marco Traverso

    Jon Bell Guest

    In article <mwnAb.9931$>,
    Marco Traverso <> wrote:
    >
    >This may seem quite a basic question...


    Yes, in fact it's in the FAQ. ;-)

    >Is there a direct function to convert a double into a string?


    There is no standard function to convert a double into a variable of the
    standard 'string' data type, but you can easily write one to do so. See

    <http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.1>

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Dec 6, 2003
    #5
  6. Thank you all for your replies!
    I didn't know about the FAQ, so the next time I'll search through them
    before posting! :)

    Thanx again,

    Marco


    "Jon Bell" <> ha scritto nel messaggio
    news:bqt47p$5bk$...
    > In article <mwnAb.9931$>,
    > Marco Traverso <> wrote:
    > >
    > >This may seem quite a basic question...

    >
    > Yes, in fact it's in the FAQ. ;-)
    >
    > >Is there a direct function to convert a double into a string?

    >
    > There is no standard function to convert a double into a variable of the
    > standard 'string' data type, but you can easily write one to do so. See
    >
    >

    <http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-38.1>
    >
    > --
    > Jon Bell <> Presbyterian College
    > Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Marco Traverso, Dec 7, 2003
    #6
    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. Der Andere

    Double -> C-String conversion

    Der Andere, Apr 26, 2004, in forum: C++
    Replies:
    16
    Views:
    27,133
    L Russ
    Apr 27, 2004
  2. kaede
    Replies:
    1
    Views:
    497
    David Hilsee
    Sep 4, 2004
  3. Sydex
    Replies:
    12
    Views:
    6,505
    Victor Bazarov
    Feb 17, 2005
  4. AviraM
    Replies:
    2
    Views:
    6,385
    Manish Pandit
    Sep 28, 2006
  5. J.M.
    Replies:
    5
    Views:
    785
Loading...

Share This Page