C++ Converting Strings and Numbers
In this page:
std::string text = std::to_string(number);
int number = std::stoi(text);
Converting a Number to a String
std::to_string converts any of C++'s built-in numeric types into a std::string, producing its standard text representation, ready to be concatenated or displayed.
Example: Converting a Number to a String
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
std::string text = std::to_string(age);
// Print to the console with cout
std::cout << text << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Converting a String to an int
stoi (string to int) parses the leading numeric portion of a string and returns it as an int, throwing an exception if the string doesn't start with a valid number.
Example: Converting a String to an int
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string input = "42";
// Declare number and set it to std::stoi(input)
int number = std::stoi(input);
// Print to the console with cout
std::cout << number + 1 << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Converting a String to a double
stod (string to double) works like stoi but parses a floating-point number, returning a double, useful for reading decimal values that were originally entered or stored as text.
Example: Converting a String to a double
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string input = "3.14";
// Declare value and set it to std::stod(input)
double value = std::stod(input);
// Print to the console with cout
std::cout << value << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Handling Invalid Conversions
Both stoi and stod throw a std::invalid_argument exception if the string doesn't begin with a valid number at all, so conversions from untrusted input should be wrapped in a try-catch block.
Example: Handling Invalid Conversions
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string bad = "abc";
// Try code that might throw an exception
try {
// Declare number and set it to std::stoi(bad)
int number = std::stoi(bad);
// Print to the console with cout
std::cout << number << std::endl;
// Handle the exception if one was thrown
} catch (std::invalid_argument &e) {
// Print to the console with cout
std::cout << "Invalid number" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Formatting Numbers as Strings
Combining to_string with string concatenation is a common way to build a formatted message that mixes fixed text with computed numeric values.
Example: Formatting Numbers as Strings
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
std::string message = "Your score: " + std::to_string(score);
// Print to the console with cout
std::cout << message << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Calling
stoi("abc"), which throwsinvalid_argument. - Using
atoiand being unable to tell 0 from an error. - Forgetting that
to_stringneeds<string>.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings