stringstream

F

Flzw

I can't seem to find how to set fixed 2 digits after the dot format to float
and double numbers, I get "<< operator is ambiguous " if I try using
setprecision and ios::fixed...any help ?
 
V

Victor Bazarov

Flzw said:
I can't seem to find how to set fixed 2 digits after the dot format to float
and double numbers, I get "<< operator is ambiguous " if I try using
setprecision and ios::fixed...any help ?

You're on the right track. What's the code you have a problem with?

#include <iostream>
#include <iomanip>

int main()
{
float pi = 3.1415926f;
std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
std::cout << std::fixed << std::setprecision(4) << pi << std::endl;
}

Prints
3.14
3.1416
 
F

Flzw

I can't seem to find how to set fixed 2 digits after the dot format to
You're on the right track. What's the code you have a problem with?

#include <iostream>
#include <iomanip>

int main()
{
float pi = 3.1415926f;
std::cout << std::fixed << std::setprecision(2) << pi << std::endl;
std::cout << std::fixed << std::setprecision(4) << pi << std::endl;
}

Prints
3.14
3.1416

Well yes I know how to do that with iostream / cin / cout but I want
something similar with a ostringstream, I was'nt explicit enough, sorry ; )
There is no setf() member function for an ostreamstring either....
 
V

Victor Bazarov

Flzw said:
Well yes I know how to do that with iostream / cin / cout but I want
something similar with a ostringstream, I was'nt explicit enough, sorry ; )
There is no setf() member function for an ostreamstring either....

What are you talking about???

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
float pi = 3.1415926f;
std::eek:stringstream os;
os << std::fixed << std::setprecision(2) << pi << std::endl;
os << std::fixed << std::setprecision(4) << pi << std::endl;
std::cout << os.str();
}

Prints the same thing. That's the beauty of streams.
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top