How to Convert a String to int or float

beginner c++11 strings

Related: int or float to string

The C++ standard library provides the “stoX” family of functions for converting strings to numbers of various types. Leading whitespace is ignored and any trailing non-number characters after a number is identified stops the processing and the number is returned. This works for integer numbers as well as floating point numbers. Here’s an example:

#include <string>
#include <iostream>

int main() {
  std::string s("-100");
  int i = std::stoi(s);
  std::cout << i << "\n";

  std::string s2("   -2021    ");
  long l = std::stol(s2);
  std::cout << l << "\n";
  
  std::string s3("     1234567890morewords");
  unsigned long ll = std::stoul(s3);
  std::cout << ll << "\n";
  
  std::string s4("3.1415926535");
  float pi = std::stof(s4);
  std::cout << pi << "\n";

  std::string s5("9.10938356e-31kg");
  double massOfElectron= std::stof(s5);
  std::cout << massOfElectron << "\n";
}
-100
-2021
1234567890
3.14159
9.10938e-31

Integers API

               int std::stoi(...)
              long std::stol(...)
         long long std::stoll(...)
     unsigned long std::stoul(...)
unsigned long long std::stoull(...)

// Where (...) is:
(const std::string& s, size_t* pos = nullptr, int base = 10)

Floating Point API

      float std::stof(...)
     double std::stod(...)
long double std::stold(...)

// Where (...) is:
(const std::string& s, size_t* pos = nullptr)

Optional Arguments

Each function has an optional out parameter (size_t* pos) if you’d like to know how many characters were processed. This is useful if you want to use the remainder of the string for something else. The integer functions have a third parameter (int base) to specify the base of the number which can be 0 or from 2 to 36.

#include <string>
#include <iostream>

int main() {
  // Binary for "14" and extra words that won't be processed
  std::string s("1110Mars is 1.5AU from the Sun.");

  size_t numProcessed;
  int i = std::stoi(s, &numProcessed, 2); // base 2

  std::string remainder = s.substr(numProcessed);

  std::cout << i << "\n";
  std::cout << "Processed " << numProcessed << " characters\n";
  std::cout << "Remainder: \"" << remainder << "\"\n";
}
14
Processed 4 characters
Remainder: "Mars is 1.5AU from the Sun."

Exceptions

If std::stoi and friends can’t parse any number from the string the std::invalid_argument exception is raised. If there is a number but it won’t fit into the maximum value allowed by the return type std::out_of_range will be thrown.

#include <string>
#include <iostream>

int main() {
  std::string s("Not an int 123");
  try {
    int i = std::stoi(s);
    std::cout << i << "\n";
  } catch (const std::invalid_argument& e) {
    std::cout << e.what() << "\n";
  }

  s = "9999999999999";
  try {
    int i = std::stoi(s);
    std::cout << i << "\n";
  } catch (const std::out_of_range& e) {
    std::cout << e.what() << "\n";
  }
}
stoi: no conversion
stoi: out of range


For more C++ By Example, click here.