A double value can be converted into a string when numeric data needs to be stored or processed as text. C++ provides several approaches for this conversion, including to_string(), stringstream, sprintf(), and boost::lexical_cast().
- to_string() is the simplest standard C++ approach.
- stringstream and sprintf() provide more control over formatting and precision.
Examples:
Input: 456321.7651234
Output: "456321.765123"Input: 2332.43
Output: "2332.43"
Methods to Convert Double to String in C++
The conversion can be performed using the following methods:
1. Using to_string()
The std::to_string() function converts a numeric value into its string representation. It is the simplest approach when no special formatting is required.
#include <iostream>
#include <string>
using namespace std;
int main()
{
double n = 456321.7651234;
// Convert double to string
string str = to_string(n);
cout << "String is: " << str;
return 0;
}
Output
String is: 456321.765123
Explanation
- to_string() converts the double value into a string.
- The resulting string can be stored, displayed, or used in further string operations.
- The default formatting may limit the number of digits shown after the decimal point.
2. Using stringstream
A stringstream or ostringstream can convert a double into a string while also providing formatting options such as precision.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
double n = 2332.43;
ostringstream stream;
// Insert double value into the stream
stream << n;
// Get the resulting string
string str = stream.str();
cout << "String is: " << str;
return 0;
}
Output
String is: 2332.43
Explanation
- ostringstream treats the double value as a stream of characters.
- The << operator inserts the value into the stream.
- str() extracts the converted value as a string.
3. Using sprintf()
The sprintf() function converts a double into a C-style character array. It is useful when custom formatting or precision is required.
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
double n = 1243.3456;
char str[50];
// Convert double to a formatted C-style string
sprintf(str, "%.4f", n);
cout << "String is: " << str;
return 0;
}
Output
string is: 1243.345600
Explanation
- sprintf() formats the double value according to the specified format.
- %.4f keeps exactly four digits after the decimal point.
- The result is stored in the character array str.
4. Using boost::lexical_cast()
boost::lexical_cast() converts values between strings and other data types. It requires the Boost library and can be used when Boost is already part of the project.
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
double n = 432.12;
// Convert double to string
string str = boost::lexical_cast<string>(n);
cout << "String is: " << str;
return 0;
}
Output
string is:432.12
Explanation
- boost::lexical_cast<string>() converts the double value into a string.
- The converted value is stored in str.
- This approach is useful when the Boost library is already being used in the project.