convert double to string query

Discussion in 'C++' started by jeff_zhang446@yahoo.com, Sep 23, 2005.

  1. Guest

    Hi,

    I try to convert double to string as below:


    std::string cnvrtToString(double lValue)
    {
    std::eek:stringstream lStream;
    lStream << lValue;
    return lStream.str();
    }

    however I found that I have lost the accuracy(having lesser decimal
    value) of my result after I print it to an outfile by doing
    fprintf(File, "%s", stream.ctr());

    I am not able to print directly:
    fprintf(File, "%f", lValue); as I need to format my outfile
    appropriately.

    I wonder if the lost of accuracy happen when I convert double to
    string? If yes, is there anyway I can avoid that?

    Please correct me if I am wrong.

    Thnak you.

    jeff
    , Sep 23, 2005
    #1
    1. Advertising

  2. wrote:
    > I try to convert double to string as below:
    >
    >
    > std::string cnvrtToString(double lValue)
    > {
    > std::eek:stringstream lStream;
    > lStream << lValue;
    > return lStream.str();
    > }
    >
    > however I found that I have lost the accuracy(having lesser decimal
    > value) of my result after I print it to an outfile by doing
    > fprintf(File, "%s", stream.ctr());
    >
    > I am not able to print directly:
    > fprintf(File, "%f", lValue); as I need to format my outfile
    > appropriately.
    >
    > I wonder if the lost of accuracy happen when I convert double to
    > string? If yes, is there anyway I can avoid that?


    Using your terms, yes, it does. The default "accuracy" is 6 digits
    after the decimal point. If your value is around 1, you lose more
    than half the digits. You need 'setprecision(16)' (for most double
    representations out there).

    V
    Victor Bazarov, Sep 24, 2005
    #2
    1. Advertising

  3. Guest

    could you please explain in more detail how and where to include the
    setprecision?

    thanks

    jeff
    , Sep 24, 2005
    #3
  4. Mike Wahler Guest

    <> wrote in message
    news:...
    > could you please explain in more detail how and where to include the
    > setprecision?


    Insert it into the stream.

    #include <iomanip>
    #include <iostream>

    int main()
    {
    double d(1.23456789);
    std::cout << std::setprecision(16);
    std::cout << d << '\n';
    return 0;
    }

    -Mike
    Mike Wahler, Sep 24, 2005
    #4
    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. da Vinci
    Replies:
    5
    Views:
    8,704
    da Vinci
    Jul 31, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,453
    Victor Bazarov
    Feb 17, 2005
  3. AviraM
    Replies:
    2
    Views:
    6,330
    Manish Pandit
    Sep 28, 2006
  4. Heiko Vogel
    Replies:
    3
    Views:
    547
    Method Man
    Sep 14, 2004
  5. nick
    Replies:
    1
    Views:
    441
    David Mark
    Feb 13, 2011
Loading...

Share This Page