streams and floaters

N

Noah Roberts

I have a double that contains the value 1000000. I want to print
"1000000". Just doing this:

str << doub;

results in "1e+006". Attempting this:

str << std::fixed << doub;

results in "1000000.000000"

str << std::fixed << std::noshowpoint << doub

results in the same.
From what I am reading, the last should be exactly what I want but it
isn't resulting in what I want. What is the correct way?
 
B

BRG

Noah said:
I have a double that contains the value 1000000. I want to print
"1000000". Just doing this:

str << doub;

results in "1e+006". Attempting this:

str << std::fixed << doub;

results in "1000000.000000"

str << std::fixed << std::noshowpoint << doub

results in the same.

isn't resulting in what I want. What is the correct way?

str << std::fixed << std::noshowpoint << std::setprecision(0) << doub

should work.

Brian Gladman
 
N

Noah Roberts

BRG said:
str << std::fixed << std::noshowpoint << std::setprecision(0) << doub

Doesn't setprecision(0) also shut off after the decimal if I have a
value that needs it?
 
B

BRG

Noah said:
Doesn't setprecision(0) also shut off after the decimal if I have a
value that needs it?

Yes.

If this matters to you you will need to test if your number is an
integer and set the argument to setprecision accordingly. Something like:

str << std::fixed << std::noshowpoint
<< std::setprecision(x == std::floor(x) ? 0 : 5) << x;

Brian Gladman
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top