Convert Double to String
Convert Double to String in C++ (6 Programs)
C++ Convert Double to String Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main() {
double num = 123.456;
// Method 1: Using to_string()
string str1 = to_string(num);
// Method 2: Using stringstream with precision
stringstream ss;
ss << fixed << setprecision(2) << num;
string str2 = ss.str();
cout << "Double: " << num << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;
return 0;
}Double: 123.456 String (method 1): 123.456000 String (method 2): 123.46
Understanding Convert Double to String
This program teaches you how to convert a double (floating-point number) into a string in C++. This conversion is essential when you need to display decimal numbers as text, format output with specific precision, write numbers to files, or combine numbers with strings. Understanding different conversion methods, especially for controlling decimal precision, is crucial for professional output formatting.
---
1. What This Program Does
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:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
double num = 123.456;
---
4. 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)
---
5. 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"
---
6. Other Methods (Mentioned but not shown in code)
Method 3: Using sprintf() (C-style)
char buffer[50];
sprintf(buffer, "%.2f", num);
string str3 = buffer;
#include <cstdio>Method 4: Using ostringstream
ostringstream oss;
oss << fixed << setprecision(2) << num;
string str4 = oss.str();
#include <sstream>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 codeMethod 6: Using Boost Library
---
#include <boost/lexical_cast.hpp>7. 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"
---
8. Displaying Results
The program prints:
Output:
Method 1 shows default precision (6 decimals), Method 2 shows formatted precision (2 decimals).
---
cout << "Double: " << num << endl;
cout << "String (method 1): " << str1 << endl;
cout << "String (method 2): " << str2 << endl;9. 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
---
10. 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
---
11. 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"
---
12. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning how to format decimal numbers, control output precision, and create professional-looking displays in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Convert Double to String
This C++ program is part of the "String Conversion Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.