Convert String to Double
Convert String to Double in C++ (6 Programs)
C++ Convert String to Double Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string str = "123.456";
// Method 1: Using stod()
double num1 = stod(str);
// Method 2: Using stringstream
stringstream ss(str);
double num2;
ss >> num2;
cout << "String: " << str << endl;
cout << "Double (method 1): " << num1 << endl;
cout << "Double (method 2): " << num2 << endl;
return 0;
}String: 123.456 Double (method 1): 123.456 Double (method 2): 123.456
Understanding Convert String to Double
This program teaches you how to convert a string containing a decimal number into a double (floating-point number) in C++. This conversion is essential when reading decimal numbers from user input, files, or external data sources. Understanding different conversion methods helps you handle decimal numbers accurately and safely in your programs.
---
1. What This Program Does
The program converts a string like "123.456" into a double value 123.456. This conversion is necessary because:
Example:
---
2. Header Files Used
---
3. Declaring Variables
The program declares:
string str = "123.456";
---
4. Method 1: Using stod() (String to Double)
double num1 = stod(str);
This is the most modern and recommended method in C++:
How it works:
Advantages:
Example:
double d1 = stod("123.456"); // 123.456
double d2 = stod("1.23e2"); // 123.0 (scientific notation)
double d3 = stod(" 45.67 "); // 45.67 (whitespace handled)
---
5. Method 2: Using stringstream
stringstream ss(str);
double num2;
ss >> num2;
This method uses stringstream, which treats a string like an input stream:
How it works:
Advantages:
Example for multiple values:
stringstream ss("123.45 67.89 10.11");
double a, b, c;
ss >> a >> b >> c; // a=123.45, b=67.89, c=10.11
---
6. Other Methods (Mentioned but not shown in code)
Method 3: Using atof() (C-style)
double num3 = atof(str.c_str());
#include <cstdlib>Method 4: Using sscanf() (C-style)
double num4;
sscanf(str.c_str(), "%lf", &num4);
#include <cstdio>Method 5: Using strtod() (C-style)
char* endPtr;
double num5 = strtod(str.c_str(), &endPtr);
#include <cstdlib>Method 6: Manual Conversion
double num6 = 0.0;
double sign = 1.0;
int i = 0;
if (str[0] == '-') { sign = -1.0; i = 1; }
// Convert integer part
while (i < str.length() && str[i] != '.') {
num6 = num6 * 10 + (str[i] - '0');
i++;
}
// Convert decimal part
if (i < str.length() && str[i] == '.') {
i++;
double fraction = 0.1;
while (i < str.length()) {
num6 += (str[i] - '0') * fraction;
fraction /= 10;
i++;
}
}
num6 *= sign;
---
// Handle negative sign7. How Manual Conversion Works (Step-by-Step)
Understanding manual conversion helps you understand the concept:
For string "123.456":
The process:
---
8. Displaying Results
The program prints:
Output:
Both methods produce the same double value.
---
cout << "String: " << str << endl;
cout << "Double (method 1): " << num1 << endl;
cout << "Double (method 2): " << num2 << endl;9. Precision and Accuracy
Double precision
: double can represent approximately 15-17 significant digits
Floating-point limitations
:
For exact decimal arithmetic
: Consider using decimal libraries or fixed-point arithmetic for financial applications.
---
10. Error Handling
Different methods handle errors differently:
stod()
try {
double num = stod("abc");
}
// Handle erroratof()
: Returns 0.0 for invalid input (no way to distinguish error from actual 0.0)
strtod()
: Sets endPtr to indicate where conversion stopped
char* endPtr;
double num = strtod("123.45abc", &endPtr);
if (*endPtr != '