std::stod, std::stof, std::stold in C++ - GeeksforGeeks

29 Oct, 2018
std::stod() : It convert string into double.Syntax:double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
Return Value: return a value of type double
Parameters
str : the string to convert
pos : address of an integer to store the 
number of characters processed. This parameter can also be 
a null pointer, in which case it is not used.
// CPP program to illustrate// std::stod()#include <string>#include <iostream>  int main(void)  {    std::string str = "y=4.4786754x+5.6";      double y, x, a, b;    y = 0;    x = 0;      // offset will be set to the length of     // characters of the "value" - 1.    std::size_t offset = 0;      a = std::stod(&str[2], &offset);    b = std::stod(&str[offset + 3]);      std::cout << b;    return 0;}Output:5.6
Another Example :// CPP program to illustrate// std::stod()#include <iostream>#include <string>using namespace std;int main(){      string b = "5";    double a = stod(b);    int c = stoi(b);    cout << b << " " << a << " " << c << endl;}Output:5 5 5
If conversion is not performed, an invalid_argument exception is thrown. If the value read is out of the range of representable values by a double an out_of_range exception is thrown. An invalid idx causes undefined behavior.std::stof : It convert string into float.Syntax:float stof( const string& str, size_t* pos = 0 );
float stof( const wstring& str, size_t* pos = 0 );
Parameters
str : the string to convert
pos : address of an integer to store the number of characters processed
This parameter can also be a null pointer, in which case it is not used.
Return value: it returns value of type float.Example 1:// CPP program to illustrate// std::stof()#include <iostream>#include <string>int main(){    std::string x;    x = "20";    float y = std::stof(x) + 2.5;    std::cout << y;    return 0;}Output:22.5
Example 2:// CPP program to illustrate// std::stof()#include <iostream>#include <string>int main(){    std::string str = "5000.5";    float x = std::stof(str);    std::cout << x;    return 0;}Output:5000.5
If no conversion could be performed, an invalid_argument exception is thrown.std::stold : It convert string into long double.Syntax:long double stold( const string& str, size_t *pos = 0 );
long double stold (const wstring& str, size_t* pos = 0);
Parameters : 
str : the string to convert
pos : address of integer to store the index of the first unconverted character.
This parameter can also be a null pointer, in which case it is not used.
Return value : it returns value of type long double.
Examples 1:// CPP program to illustrate// std::stold()#include <iostream>#include <string>int main(){    std::string str = "500087";    long double x = std::stold(str);    std::cout << x;    return 0;}Output:500087
Example 2:// CPP program to illustrate// std::stold()#include <iostream>#include <string>int main(){    std::string x;    x = "2075";    long double y = std::stof(x) + 2.5;    std::cout << y;    return 0;}Output:2077.5

std::stod() : It convert string into double.Syntax:double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
Return Value: return a value of type double
Parameters
str : the string to convert
pos : address of an integer to store the 
number of characters processed. This parameter can also be 
a null pointer, in which case it is not used.
// CPP program to illustrate// std::stod()#include <string>#include <iostream>  int main(void)  {    std::string str = "y=4.4786754x+5.6";      double y, x, a, b;    y = 0;    x = 0;      // offset will be set to the length of     // characters of the "value" - 1.    std::size_t offset = 0;      a = std::stod(&str[2], &offset);    b = std::stod(&str[offset + 3]);      std::cout << b;    return 0;}Output:5.6
Another Example :// CPP program to illustrate// std::stod()#include <iostream>#include <string>using namespace std;int main(){      string b = "5";    double a = stod(b);    int c = stoi(b);    cout << b << " " << a << " " << c << endl;}Output:5 5 5
If conversion is not performed, an invalid_argument exception is thrown. If the value read is out of the range of representable values by a double an out_of_range exception is thrown. An invalid idx causes undefined behavior.
double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
Return Value: return a value of type double
Parameters
str : the string to convert
pos : address of an integer to store the 
number of characters processed. This parameter can also be 
a null pointer, in which case it is not used.

// CPP program to illustrate// std::stod()#include <string>#include <iostream>  int main(void)  {    std::string str = "y=4.4786754x+5.6";      double y, x, a, b;    y = 0;    x = 0;      // offset will be set to the length of     // characters of the "value" - 1.    std::size_t offset = 0;      a = std::stod(&str[2], &offset);    b = std::stod(&str[offset + 3]);      std::cout << b;    return 0;}
Output:
5.6

Another Example :
// CPP program to illustrate// std::stod()#include <iostream>#include <string>using namespace std;int main(){      string b = "5";    double a = stod(b);    int c = stoi(b);    cout << b << " " << a << " " << c << endl;}
Output:
5 5 5

If conversion is not performed, an invalid_argument exception is thrown. If the value read is out of the range of representable values by a double an out_of_range exception is thrown. An invalid idx causes undefined behavior.
std::stof : It convert string into float.Syntax:float stof( const string& str, size_t* pos = 0 );
float stof( const wstring& str, size_t* pos = 0 );
Parameters
str : the string to convert
pos : address of an integer to store the number of characters processed
This parameter can also be a null pointer, in which case it is not used.
Return value: it returns value of type float.Example 1:// CPP program to illustrate// std::stof()#include <iostream>#include <string>int main(){    std::string x;    x = "20";    float y = std::stof(x) + 2.5;    std::cout << y;    return 0;}Output:22.5
Example 2:// CPP program to illustrate// std::stof()#include <iostream>#include <string>int main(){    std::string str = "5000.5";    float x = std::stof(str);    std::cout << x;    return 0;}Output:5000.5
If no conversion could be performed, an invalid_argument exception is thrown.
float stof( const string& str, size_t* pos = 0 );
float stof( const wstring& str, size_t* pos = 0 );
Parameters
str : the string to convert
pos : address of an integer to store the number of characters processed
This parameter can also be a null pointer, in which case it is not used.
Return value: it returns value of type float.
Example 1:
// CPP program to illustrate// std::stof()#include <iostream>#include <string>int main(){    std::string x;    x = "20";    float y = std::stof(x) + 2.5;    std::cout << y;    return 0;}
Output:
22.5

Example 2:
// CPP program to illustrate// std::stof()#include <iostream>#include <string>int main(){    std::string str = "5000.5";    float x = std::stof(str);    std::cout << x;    return 0;}
Output:
5000.5

If no conversion could be performed, an invalid_argument exception is thrown.
std::stold : It convert string into long double.Syntax:long double stold( const string& str, size_t *pos = 0 );
long double stold (const wstring& str, size_t* pos = 0);
Parameters : 
str : the string to convert
pos : address of integer to store the index of the first unconverted character.
This parameter can also be a null pointer, in which case it is not used.
Return value : it returns value of type long double.
Examples 1:// CPP program to illustrate// std::stold()#include <iostream>#include <string>int main(){    std::string str = "500087";    long double x = std::stold(str);    std::cout << x;    return 0;}Output:500087
Example 2:// CPP program to illustrate// std::stold()#include <iostream>#include <string>int main(){    std::string x;    x = "2075";    long double y = std::stof(x) + 2.5;    std::cout << y;    return 0;}Output:2077.5

long double stold( const string& str, size_t *pos = 0 );
long double stold (const wstring& str, size_t* pos = 0);
Parameters : 
str : the string to convert
pos : address of integer to store the index of the first unconverted character.
This parameter can also be a null pointer, in which case it is not used.
Return value : it returns value of type long double.

Examples 1:
// CPP program to illustrate// std::stold()#include <iostream>#include <string>int main(){    std::string str = "500087";    long double x = std::stold(str);    std::cout << x;    return 0;}
Output:
500087

Example 2:
// CPP program to illustrate// std::stold()#include <iostream>#include <string>int main(){    std::string x;    x = "2075";    long double y = std::stof(x) + 2.5;    std::cout << y;    return 0;}
Output:
2077.5

This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
papun007
CPP-Library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Map in C++ Standard Template Library (STL)
Inheritance in C++
Constructors in C++
C++ Classes and Objects
Socket Programming in C/C++
Bitwise Operators in C/C++
Operator Overloading in C++
Copy Constructor in C++
Virtual Function in C++
Templates in C++ with Examples