English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to in C / C ++How to convert a string to double precision in

This is an example of converting a string to double.

Example

#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 result

The number after conversion of string: 18.289400

In the above program, a char type array s [20]. This functionstrtod()used to convert the string to a double.

char s[20] = "18.2894 is a number";
char *p;
double result;
result = strtod(s, &p);