How to convert a std::string to double

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout << stod("  99.999  ") << endl;
}

Output: 99.999 (which is double, whitespace was automatically stripped)

Since C++11 converting string to floating-point values (like double) is available with functions:
stof – convert str to a float
stod – convert str to a double
stold – convert str to a long double

As conversion of string to int was also mentioned in the question, there are the following functions in C++11:
stoi – convert str to an int
stol – convert str to a long
stoul – convert str to an unsigned long
stoll – convert str to a long long
stoull – convert str to an unsigned long long

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)