Convert Double to String in C++
Convert a double to a string in C++ with to_string and with a stringstream using fixed and setprecision. Explains why to_string(123.456) gives 123.456000.
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}
Sample Output
String (method 1): 123.456000
to_string pads to six decimals.
String (method 2): 123.46
Rounded to two decimals.
In this one
- That to_string always uses six digits after the decimal point
- How a stringstream with fixed and setprecision controls digits
- How rounding happens at the last kept digit
- When each method fits
How it works
- 1
num holds 123.456.
- 2
to_string gives "123.456000".
- 3
A stringstream set to fixed with precision 2 writes "123.46".
- 4
ss.str() returns that text.
- 5
Print all three.
Edge cases
Tiny value
Convert 0.0000001 with to_string.
to_string gives "0.000000", because it keeps only six decimal places. Use a stringstream with more precision or scientific format.
Rounding halfway
Convert 2.675 to two places.
You may get 2.67, not 2.68, because 2.675 is stored as slightly less than 2.675 in binary.
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)
#include <cstdio>
char buffer[50];
sprintf(buffer, "%.2f", num);
string str3 = buffer;
Method 4: Using ostringstream
#include <sstream>
ostringstream oss;
oss << fixed << setprecision(2) << num;
string str4 = oss.str();
Method 5: Manual Conversion
string str5 = "";
// Complex manual conversion handling integer and decimal parts
// Extracts digits and formats with decimal point
// Most control but most code
Method 6: Using Boost Library
#include <boost/lexical_cast.hpp>
string str6 = boost::lexical_cast<string>(num);
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:
cout << "Double: " << num << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
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).
When to Use Each Method
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.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does to_string(1.5) return?
Pick an answer to check it.
Question 2
With fixed and setprecision(1), how is 9.96 written?
Pick an answer to check it.
Questions people ask
How do I drop trailing zeros?
Use a stringstream without fixed, or trim the zeros from the string afterwards.
Does setprecision change the number?
No. It only changes how the number is written as text.
Is there a C++20 option?
std::format("{:.2f}", num) in <format> does the same with less code, where your compiler supports it.