VOOZH about

URL: https://www.geeksforgeeks.org/cpp/stdstod-stdstof-stdstold-c/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 29 Oct, 2018
  1. 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.
    
    Output:
    5.6
    
    Another Example : 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.
  2. 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: Output:
    22.5
    
    Example 2: Output:
    5000.5
    
    If no conversion could be performed, an invalid_argument exception is thrown.
  3. 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: Output:
    500087
    
    Example 2:
Output:
2077.5
Comment
Article Tags:
Article Tags: