How to prevent a double from appearing in e-notation?

Discussion in 'C++' started by jeff_j_dunlap@yahoo.com, Aug 23, 2007.

  1. Guest

    I am trying to display some numbers but they are displaying in e-
    notation as seen in the output below.

    double a = 500000;
    double b = 1400000;
    cout << "a = " << a << endl; // prints "a = 500000"
    cout << "b = " << b << endl; // prints "b = 1.4e+006"


    Here is the actual reason for my concern. From within my web
    application the output being displayed in notated form as well:

    // double to string conversion
    std::string DoubleToStr(double x)
    {
    std::eek:stringstream o;
    if ((o << x))
    return o.str();
    else
    return "";
    }

    // convert and output string value
    cout << DoubleToString(a); // prints "a = 500000"
    cout << DoubleToString(b); // prints "b = 1.4e+006"


    Would someone tell me how not to display the data in notated form?

    Thank you,

    Jeff
    , Aug 23, 2007
    #1
    1. Advertising

  2. wrote:
    > I am trying to display some numbers but they are displaying in e-
    > notation as seen in the output below.
    >
    > double a = 500000;
    > double b = 1400000;
    > cout << "a = " << a << endl; // prints "a = 500000"
    > cout << "b = " << b << endl; // prints "b = 1.4e+006"


    Use the 'fixed' manipulator. See the <iomanip> header (IIRC).

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 23, 2007
    #2
    1. Advertising

  3. red floyd Guest

    wrote:
    > I am trying to display some numbers but they are displaying in e-
    > notation as seen in the output below.
    >
    > double a = 500000;
    > double b = 1400000;
    > cout << "a = " << a << endl; // prints "a = 500000"
    > cout << "b = " << b << endl; // prints "b = 1.4e+006"
    >
    >
    > Here is the actual reason for my concern. From within my web
    > application the output being displayed in notated form as well:
    >
    > // double to string conversion
    > std::string DoubleToStr(double x)
    > {
    > std::eek:stringstream o;
    > if ((o << x))

    if (o << std::fixed << x)
    > return o.str();
    > else
    > return "";
    > }
    >
    > // convert and output string value
    > cout << DoubleToString(a); // prints "a = 500000"
    > cout << DoubleToString(b); // prints "b = 1.4e+006"
    >
    >
    > Would someone tell me how not to display the data in notated form?
    >


    Use std::fixed. See fix to DoubleToString().
    red floyd, Aug 23, 2007
    #3
  4. Guest

    > Use std::fixed. See fix to DoubleToString().


    Red, Victor:

    Thank you kindly for pointing me in the right direction. I am now
    able to display rounded data type as std::sting:


    inline double fround(double n, double d)
    {
    return floor(n * pow(10., d) + .5) / pow(10., d);
    }

    std::string IntToStr(double x, int y=2) // default to two decimal
    places
    {
    std::eek:stringstream o;
    if (o << std::fixed << fround(x,y))
    return o.str().substr(0, o.str().find('.')+y+1 ) ;
    else
    return "";
    }
    , Aug 23, 2007
    #4
  5. red floyd Guest

    wrote:
    >> Use std::fixed. See fix to DoubleToString().

    >
    >
    > Red, Victor:
    >
    > Thank you kindly for pointing me in the right direction. I am now
    > able to display rounded data type as std::sting:
    >
    >
    > inline double fround(double n, double d)
    > {
    > return floor(n * pow(10., d) + .5) / pow(10., d);
    > }
    >
    > std::string IntToStr(double x, int y=2) // default to two decimal
    > places
    > {
    > std::eek:stringstream o;
    > if (o << std::fixed << fround(x,y))
    > return o.str().substr(0, o.str().find('.')+y+1 ) ;
    > else
    > return "";
    > }



    You might want to look at std::setprecision as well. See
    http://www.dinkumware.com/manuals/?manual=compleat&page=iomanip.html#setprecision
    red floyd, Aug 23, 2007
    #5
    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. =?Utf-8?B?VmFkaW0=?=
    Replies:
    1
    Views:
    375
    =?Utf-8?B?VmFkaW0=?=
    Jan 20, 2005
  2. Sydex
    Replies:
    12
    Views:
    6,485
    Victor Bazarov
    Feb 17, 2005
  3. Grey Squirrel

    Hungarian Notation Vs. Pascal Notation?

    Grey Squirrel, Mar 19, 2007, in forum: ASP .Net
    Replies:
    6
    Views:
    1,300
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 21, 2007
  4. Tameem
    Replies:
    454
    Views:
    11,810
  5. Robert Mark Bram

    Dot notation V Bracket notation

    Robert Mark Bram, Jul 4, 2003, in forum: Javascript
    Replies:
    3
    Views:
    467
    Robert Mark Bram
    Jul 5, 2003
Loading...

Share This Page