Convert Double to String in C++
Convert Double to String in C++ (6 Programs).
1#include <iostream>2#include <string>3#include <sstream>4#include <iomanip>5using namespace std;67int main() {8 double num = 123.456;910 // Method 1: Using to_string()11 string str1 = to_string(num);1213 // Method 2: Using stringstream with precision14 stringstream ss;15 ss << fixed << setprecision(2) << num;16 string str2 = ss.str();1718 cout << "Double: " << num << endl;19 cout << "String (method 1): " << str1 << endl;20 cout << "String (method 2): " << str2 << endl;2122 return 0;23}
Output
Double: 123.456 String (method 1): 123.456000 String (method 2): 123.46
What's going on
The program converts a double (like 123.456) into a string (like "123.456" or "123.46" with formatting). This conversion is necessary because:
Example:
Header Files Used
Declaring Variables
The program declares:
double num = 123.456;
Method 1: Using to_string() (Double to String)
string str1 = to_string(num);
This is a simple method, but has limitations:
How it works:
Advantages:
Limitations:
Example:
double d = 123.456;
string s = to_string(d); // s = "123.456000" (6 decimals)
Method 2: Using stringstream with Precision
stringstream ss;
ss << fixed << setprecision(2) << num;
string str2 = ss.str();
This is the recommended method for formatted conversion:
How it works:
Advantages:
Example:
stringstream ss;
ss << fixed << setprecision(2) << 123.456;
string s = ss.str(); // s = "123.46"
Example with scientific notation:
stringstream ss;
ss << scientific << setprecision(3) << 123.456;
string s = ss.str(); // s = "1.235e+02"
Other Methods (Mentioned but not shown in code)
Method 3: Using sprintf() (C-style)
char buffer[50];
sprintf(buffer, "%.2f", num);
string str3 = buffer;
1#include <cstdio>
Method 4: Using ostringstream
ostringstream oss;
oss << fixed << setprecision(2) << num;
string str4 = oss.str();
1#include <sstream>
Method 5: Manual Conversion
string str5 = "";
1// Complex manual conversion handling integer and decimal parts2// Extracts digits and formats with decimal point3// Most control but most code
Method 6: Using Boost Library
string str6 = boost::lexical_cast<string>(num);
1#include <boost/lexical_cast.hpp>
Understanding Formatting Options
fixed
: Displays numbers in fixed-point notation (not scientific)
setprecision(n)
: Sets the number of decimal places
scientific
: Displays numbers in scientific notation
Combining options
:
stringstream ss;
ss << fixed << setprecision(2) << 123.456; // "123.46"
ss << scientific << setprecision(3) << 123.456; // "1.235e+02"
Displaying Results
The program prints:
Output:
Double: 123.456
String (method 1): 123.456000
String (method 2): 123.46
Method 1 shows default precision (6 decimals), Method 2 shows formatted precision (2 decimals).
1cout << "Double: " << num << endl;2cout << "String (method 1): " << str1 << endl;3cout << "String (method 2): " << str2 << endl;
When to Use Each Method
-
to_string()
: Simple cases where default precision is acceptable
-
stringstream with formatting
: Best for most cases - full control over precision and format
-
sprintf()
: Only for legacy code or when you need C compatibility
-
ostringstream
: Same as stringstream, slightly more explicit for output-only
-
Manual
: For learning or when you need custom conversion logic
-
Boost
: Avoid unless already using Boost library in your project
Precision Considerations
Default precision (to_string())
:
Formatted precision (stringstream)
:
For financial applications
: Always use formatted precision (typically 2 decimal places)
For scientific applications
: May need more precision or scientific notation
Common Formatting Patterns
Currency formatting
(2 decimal places):
stringstream ss;
ss << fixed << setprecision(2) << 123.456;
string currency = "$" + ss.str(); // "$123.46"
Percentage formatting
:
stringstream ss;
ss << fixed << setprecision(1) << 45.678;
string percent = ss.str() + "%"; // "45.7%"
Scientific notation
:
stringstream ss;
ss << scientific << setprecision(3) << 123456.789;
string sci = ss.str(); // "1.235e+05"
return 0;
This ends the program successfully.