How can I convert string to double in C/C++?

CC++Server Side Programming

Here is an example to convert a string to double.

Example

 Live Demo

#include <iostream>
using namespace std;
int main() {
   char s[20] = "18.2894 is a number";
   char *p;
   double result;
   result = strtod(s, &p);
   cout<<"The number after conversion of string : "<<result;
   return(0);
}

Output

The number after conversion of string : 18.289400

In the above program, a char type array s[20] is declared which is initialized with a alphanumeric characters. The function strtod() is used to convert that string into a double number.

char s[20] = "18.2894 is a number";
char *p;
double result;
result = strtod(s, &p);
raja
Published on 05-Nov-2018 15:37:31
Advertisements