std::setprecision and scientific format

W

Woodster

I am using the std::setprecision function to format variables of type
double in a string however I am unsure how to stop this appearing in
scientific notation.

For example

std::stringstream buffer;

buffer << setprecision(1) << 40.0 << "° C";

produces the string

04e+01° C

in buffer.

Ideally I would like this to be

40° C

but I think I will have to settle for 40.0° C if I want to cater for
other values where the single decimal place is required.

How do I use setprecision an not get scientific notation as the output?

Thanks in advance

Sean Hannan
 
S

Senthilvel Samatharman

Woodster said:
I am using the std::setprecision function to format variables of type
double in a string however I am unsure how to stop this appearing in
scientific notation.

For example

std::stringstream buffer;

buffer << setprecision(1) << 40.0 << "° C";

produces the string

04e+01° C

in buffer.

Ideally I would like this to be

40° C

but I think I will have to settle for 40.0° C if I want to cater for
other values where the single decimal place is required.

How do I use setprecision an not get scientific notation as the output?

Use the manipulator "fixed" to get rid of scientific notation..
See John Bell's example for your previous post...
buffer << fixed << setprecision(3) << 40.0 << " C";
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top