Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I convert string to double in C/C++?
Given a string like "-27.89", we can convert it into a double value such as -27.890000. In this article, we use various approaches from C/C++ library functions to perform this string into double conversion:
String to Double Conversion Using atof() Function
You can use atof() function of C library that accepts the parameter of string value to convert the string into a double.
Example
In this example, we illustrate atof() to convert string into double.
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "-27.89";
double num = atof(str);
printf("Converted number: %.5f", num);
return 0;
}
The above program produces the following output:
Converted number: -27.89000
String to Double Conversion Using strtod() Function
strtod() is a C library function that pointed to the string to convert it into a double-precision floating-point number. The use the strtod() function shows greater precision and handles a larger range of values as compared to the atof() function.
Example
Following is the example of strtod() function that shows its usage to C program.
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "123.45abc";
char* endptr;
double num = strtod(str, &endptr);
if(*endptr != '\0')
printf("Extra characters after number: %s", endptr);
else
printf("Converted number: %.2f", num);
return 0;
}
The above program produces the following output:
Extra characters after number: abc
String to Double Conversion Using stod() Function
The stod() function of C++ accepts the input parameter as string that convert the string into double return the desired result.
Example
The basic illustration of stod() function in C++ program to convert string into double.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "98.76";
double num = stod(str);
cout << "Converted number: " << num;
return 0;
}
The above program produces the following output:
Converted number: 98.76
String to Double Conversion Using stringstream() Function
The stringstream() is used to manage a string object as a stream that follows the header, like sstream and string. Here, we take a string str that holds a number in text form. Then we use stringstream() to convert this string into a double type. Finally, the converted number is printed as a double value.
Example
Following is the C++ program to show the usage of stringstream() function for the conversion of string into double.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string str = "45.67";
double num;
stringstream(str) >> num;
cout << "Converted number: " << num;
return 0;
}
The above program produces the following output:
Converted number: 45.67
Comparison of C Vs. C++ Functions
Below are the comparisons of C and C++ functions based on error handling and headers as follows:
| Method | Language | Error Handling | Header Required |
|---|---|---|---|
| atof() | C | No | stdlib.h |
| strtod() | C | Yes | stdlib.h |
| stod() | C++ | Yes | string |
| stringstream | C++ | Yes | sstream |