strtod() function in C/C++

21 Aug, 2018
The strtod() is a builtin function in C and C++ STL which interprets the contents of the string as a floating point number and return its value as a double.It sets a pointer to point to the first character after the last valid character of the string, only if there is any, otherwise it sets the pointer to null.
Syntax:
double strtod(str, &end)
Parameters:str: It specifies the string which has the representation of a floating point number.end: It is specified the parameter that refers to an already allocated object of type char*.
Return Value: It returns a double value, which is converted from a string, and 0, if no valid conversion can be performed.
Below programs illustrate the above function:
Program 1:
// C++ program to illustrate the// strtod() function#include <cstdlib>#include <iostream>  using namespace std;  int main(){    char str[] = "11.03e 0mn";    char* end;    double number;      number = strtod(str, &end);    cout << "number = " << str << endl;    cout << "double = " << number << endl;    cout << "end string = " << end << endl;      return 0;}
number = 11.03e 0mn
double = 11.03
end string = e 0mn

Program 2:
// C++ program to illustrate the// strtod() function without trailing characters#include <cstdlib>#include <iostream>  using namespace std;  int main(){    char str[] = "4.06";    char* end;    double number;      number = strtod(str, &end);    cout << "number= " << str << endl;    cout << "double= " << number << endl;      // If end is not Null    if (*end) {        cout << end;    }    // If end is Null    else {        cout << "null";    }    return 0;}
number= 4.06
double= 4.06
null

Program 3:strtod() function with exponents and hexadecimals
// C++ program to illustrate the// strtod() function with exponents and hexadecimals#include <cstdlib>#include <cstring>#include <iostream>  using namespace std;int main(){    // initialize a exponential value    char str[] = "-89.04e-3win gfg";    char* end;    double number;      number = strtod(str, &end);    cout << "str = " << str << endl;    cout << "double = " << number << endl;    cout << "end string = " << end << endl         << endl;      // initialize a new hexadecimal value    strcpy(str, "1998gupta.1204ishwar");      number = strtod(str, &end);    cout << "str = " << str << endl;    cout << "double = " << number << endl;    cout << "end string = " << end << endl;      return 0;}
str = -89.04e-3win gfg
double = -0.08904
end string = win gfg

str = 1998gupta.1204ishwar
double = 1998
end string = gupta.1204ishwar

Program 4:
// C++ program to illustrate the// strtod() function for Infinity and NaN#include <cstdlib>#include <iostream>  using namespace std;  int main(){    char* end;      cout << "Infinity"         << " to double = "          << strtod("infinity", &end) << endl;    cout << "end string = " << end << endl         << endl;      cout << "Infpqrs"         << " to double = " <<          strtod("Infpqrs", &end) << endl;    cout << "end string = " << end << endl         << endl;      cout << "NaN11x"         << " to double = "          << strtod("NaN11x", &end) << endl;    cout << "end string = " << end << endl         << endl;      return 0;}
Infinity to double = inf
end string = 

Infpqrs to double = inf
end string = pqrs

NaN11x to double = nan
end string = 11x

Program 5:strtod() function with leading whitespace
// C++ program to illustrate the// strtod() function with leading whitespace#include <cstdlib>#include <iostream>  using namespace std;  int main(){    char* end;      cout << "99.99"         << " to double = "          << strtod(" 19.99", &end) << endl;    // end pointer is set to null    cout << "end string = "          << end << endl         << endl;      // Returns 0 because of invalid conversion    cout << "xyz1.80"         << " to double = "          << strtod("xyz1.80", &end) << endl;    cout << "end string = " << end << endl         << endl;      return 0;}
99.99 to double = 19.99
end string = 

xyz1.80 to double = 0
end string = xyz1.80

C-Library
CPP-Functions
CPP-Library
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Unordered Sets in C++ Standard Template Library
What is the purpose of a function prototype?
Operators in C / C++
Exception Handling in C++
Smart Pointers in C++ and How to Use Them
Vector in C++ STL
Map in C++ Standard Template Library (STL)
Initialize a vector in C++ (7 different ways)
Set in C++ Standard Template Library (STL)
vector erase() and clear() in C++