Now the sub-strings “14.25” and “34.87” are string literals and not numbers. If you have the code,
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
- String to Long Integer Conversion
- String to Unsigned Long Integer Conversion
- String to Unsigned Long Long Integer Conversion
- String to Float Conversion
- String to Double Conversion
- String to Long Double Conversion
- Full Syntax
- Whitespace
- Conclusion
String to Integer Conversion
The following program does this:
The simplified conversion syntax to use is:
where stoi means string-to-integer. The program is:
#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:
The simplified conversion syntax to use is:
where stol means string-to-long-integer. The program is:
#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:
The simplified conversion syntax to use is:
where stoul means string-to-unsigned-long-integer. The program is:
#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:
The simplified conversion syntax to use is:
where stoul means string-to-unsigned-long-long-integer. The program is:
#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:
The simplified conversion syntax to use is:
where stof means string-to-float. The program is:
#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:
String to Double Conversion
The following program does this:
The simplified conversion syntax to use is:
where stof means string-to-float. The program is:
#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:
String to Long Double Conversion
The following program does this:
The simplified conversion syntax to use is:
where stof means string-to-float. The program is:
#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:
Full Syntax
String to Integer
The full syntax to convert a string to int is:
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:
#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 <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 <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 <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 <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 <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:
The following program illustrates how whitespaces are removed in the conversion from string to double:
#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:
The following program illustrates how whitespaces are removed in the conversion from string to long double:
#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:
The following program illustrates how whitespaces are removed in the conversion from string to int, in base 16:
#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().