using sstream
Nov 7, 2018 at 11:26pm UTC
I am new to sstream. I am also trying to convert a double to a string. I need a set precision of 3 so that is why I need to use it. I've tried a bunch of different way to set this up, so now I am turning to here for help.
1 2 3 4 5 6 7
double formatPoint(double x, double y, string format) {
stringstream stream;
stream << fixed << setprecision(3)<<"(" <<x<<"," <<y<<")" ;
string format = stream.str();
return format;
}
Nov 8, 2018 at 4:38am UTC
Hello noshkren,
It would be a good idea to edit your OP and remove the green check that tells everyone that you have your answer.
You almost have it right.
Try this:
1 2 3 4 5 6 7 8
std::string formatPoint(double x, double y)
{
std::stringstream stream;
stream << std::fixed << std::setprecision(3) << "(" << x << ", " << y << ")" ;
std::string format = stream.str();
return format;
}
I changed the return type and removed the third parameter.
Another option:
1 2 3 4 5 6
void formatPoint(double x, double y, std::string& format) // <--- string passed by reference.
{
std::stringstream stream;
stream << std::fixed << std::setprecision(3) << "(" << x << ", " << y << ")" ;
format = stream.str(); // <--- Removed type specifier.
}
Hope that helps,
Andy
Topic archived. No new replies allowed.