C++

C++ String to double Conversion

Consider the following string literal:

    "I have some books worth $14.25 and others worth $34.87."

Now the sub-strings “14.25” and “34.87” are string literals and not numbers. If you have the code,

    "14.25" + "34.87"

in C++, you will not end up with 49.12. In fact, the compiler should issue an error message. To have the result, 49.12, “14.25” has to be converted to a number type of double or float, and “34.87” has to be converted to a number type of double or float.

The title of this tutorial is “C++ String to Double Conversion”. Is your aim to convert string to double; or to do all of the following, which are related?

  • string to integer
  • string to long integer
  • string to float
  • string to double
  • string to long double

This tutorial explains all these conversions and more. What is being converted is an object in string form. The C++ string class has functions to do these. To use these functions, the string class has to be included into the program.

Article Content

String to Integer Conversion

The following program does this:

    "20" to int + "30" to int = 50 as int

The simplified conversion syntax to use is:

    stoi(str)

where stoi means string-to-integer. The program is:

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

    int main()
    {
        char str1[] = "20";
        char str2[] = "30";
        int num1 = stoi(str1);
        int num2 = stoi(str2);
        int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

String to Long Integer Conversion

The following program does this:

    "20" to long int + "30" to long int = 50 as long int

The simplified conversion syntax to use is:

    stol(str)

where stol means string-to-long-integer. The program is:

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

    int main()
    {
        char str1[] = "20";
        char str2[] = "30";
        long int num1 = stol(str1);
        long int num2 = stol(str2);
        long int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

String to Unsigned Long Integer Conversion

The following program does this:

    "20" to unsigned long int + "30" to unsigned long int = 50 as unsigned long int

The simplified conversion syntax to use is:

    stoul(str)

where stoul means string-to-unsigned-long-integer. The program is:

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

    int main()
    {
        char str1[] = "20";
        char str2[] = "30";
        unsigned long int num1 = stoul(str1);
        unsigned long int num2 = stoul(str2);
        unsigned long int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

String to Unsigned Long Long Integer Conversion

The following program does this:

    "20" to unsigned long, long int + "30" to unsigned long long int = 50 as unsigned long, long int

The simplified conversion syntax to use is:

    stoull(str)

where stoul means string-to-unsigned-long-long-integer. The program is:

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

    int main()
    {
        char str1[] = "20";
        char str2[] = "30";
        unsigned long long int num1 = stoull(str1);
        unsigned long long int num2 = stoull(str2);
        unsigned long long int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

String to Float Conversion

The following program does this:

    "14.25" to float + "34.87" to float = 49.12 as float

The simplified conversion syntax to use is:

    stof(str)

where stof means string-to-float. The program is:

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

    int main()
    {
        char str1[] = "14.25";
        char str2[] = "34.87";
        float num1 = stof(str1);
        float num2 = stof(str2);
        float result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is:

    49.12

String to Double Conversion

The following program does this:

    "14.25" to double + "34.87" to double = 49.12 as double

The simplified conversion syntax to use is:

    stod(str)

where stof means string-to-float. The program is:

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

    int main()
    {
        char str1[] = "14.25";
        char str2[] = "34.87";
        double num1 = stod(str1);
        double num2 = stod(str2);
        double result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is:

    49.12

String to Long Double Conversion

The following program does this:

    "14.25" to long double + "34.87" to long double = 49.12 as double

The simplified conversion syntax to use is:

    stold(str)

where stof means string-to-float. The program is:

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

    int main()
    {
        char str1[] = "14.25";
        char str2[] = "34.87";
        long double num1 = stold(str1);
        long double num2 = stold(str2);
        long double result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is:

    49.12

Full Syntax

String to Integer
The full syntax to convert a string to int is:

    int stoi(const string& str, size_t* idx = nullptr, int base = 10)

The second argument can be allowed as nullptr. The default for the third argument is base 10. It can be changed to some other base, such as 16.

The following program does this:

    "A" to int-base-16 + "B" to int-base-16 = 21 as int-base-10
    #include <string>
    #include <iostream>
    using namespace std;

    int main()
    {
        char str1[] = "A";
        char str2[] = "B";
        int num1 = stoi(str1, nullptr, 16);
        int num2 = stoi(str2, nullptr, 16);
        int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 21.

The other syntaxes are similarly explained.

Whitespace

Whitespaces are ‘ ’, ‘\n’, ‘\r’, ‘\f’, ‘\t’, ‘\v’. One or more of these can be in front or after a number on a string. During conversion, whitespaces are removed.

The following program illustrates how whitespaces are removed in the conversion from string to int:

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

    int main()
    {
        char str1[] = "\n 20 \n";
        char str2[] = "\t 30 \t";
        int num1 = stoi(str1);
        int num2 = stoi(str2);
        int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The following program illustrates how whitespaces are removed in the conversion from string to long int:

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

    int main()
    {
        char str1[] = "\n 20 \n";
        char str2[] = "\t 30 \t";
        long int num1 = stol(str1);
        long int num2 = stol(str2);
        long int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

The following program illustrates how whitespaces are removed in the conversion from string to unsigned long int:

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

    int main()
    {
        char str1[] = "\n 20 \n";
        char str2[] = "\t 30 \t";
        unsigned long int num1 = stoul(str1);
        unsigned long int num2 = stoul(str2);
        unsigned long int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

The following program illustrates how whitespaces are removed in the conversion from string to unsigned long long int:

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

    int main()
    {
        char str1[] = "\n 20 \n";
        char str2[] = "\t 30 \t";
        unsigned long long int num1 = stoull(str1);
        unsigned long long int num2 = stoull(str2);
        unsigned long long int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 50.

The following program illustrates how whitespaces are removed in the conversion from string to float:

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

    int main()
    {
        char str1[] = "\n 14.25 \n";
        char str2[] = "\t 34.87 \t";
        float num1 = stof(str1);
        float num2 = stof(str2);
        float result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is:

    49.12

The following program illustrates how whitespaces are removed in the conversion from string to double:

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

    int main()
    {
        char str1[] = "\n 14.25 \n";
        char str2[] = "\t 34.87 \t";
        double num1 = stod(str1);
        double num2 = stod(str2);
        double result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is:

    49.12

The following program illustrates how whitespaces are removed in the conversion from string to long double:

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

    int main()
    {
        char str1[] = "\n 14.25 \n";
        char str2[] = "\t 34.87 \t";
        long double num1 = stold(str1);
        long double num2 = stold(str2);
        long double result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is:

    49.12

The following program illustrates how whitespaces are removed in the conversion from string to int, in base 16:

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

    int main()
    {
        char str1[] = "\n A \n";
        char str2[] = "\t B \t";
        int num1 = stoi(str1, nullptr, 16);
        int num2 = stoi(str2, nullptr, 16);
        int result = num1 + num2;
        cout << result << endl;
   
        return 0;
    }

The output is 21.

Conclusion

C++ has functions to convert strings to numbers. In the conversion, whitespaces are ignored. If the string has a character that is not a digit or whitespace, the result is unreliable. The functions are in the string library and they are: stoi(), stol(), stoul(), stoll(), stoull(), stof(), stod(), and stold().

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.