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

J

jeff_j_dunlap

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
 
V

Victor Bazarov

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 said:

V
 
R

red floyd

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().
 
J

jeff_j_dunlap

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 "";
}
 
R

red floyd

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top