Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Strings and I/O

C++ stoi, stod and to_string

Convert between numbers and std::string with stoi, stol, stod, to_string: bases, the pos output, invalid_argument and out_of_range errors, and better options.

The functions std::stoi, stol, stoll, stoul, stoull, stof, stod and stold turn text into numbers. std::to_string goes the other way. All of them live in <string> and were added in C++11.

The sto* functions skip leading whitespace, read as many characters as form a valid number, and stop at the first character that does not fit. They do not require the whole string to be a number: std::stoi("42abc") returns 42. They report problems by throwing exceptions, which makes them convenient for quick code but clumsy when bad input is normal. For that case, see to_chars and from_chars.

C++
int n = std::stoi("123");          // 123
double d = std::stod("3.5e2");     // 350.0
std::string s = std::to_string(42); // "42"

Try it

C++ From text to numbers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ From numbers to text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
int       stoi (const std::string& str, std::size_t* pos = nullptr, int base = 10);
long      stol (const std::string& str, std::size_t* pos = nullptr, int base = 10);
long long stoll(const std::string& str, std::size_t* pos = nullptr, int base = 10);
unsigned long      stoul (const std::string& str, std::size_t* pos = nullptr, int base = 10);
unsigned long long stoull(const std::string& str, std::size_t* pos = nullptr, int base = 10);
float       stof (const std::string& str, std::size_t* pos = nullptr);
double      stod (const std::string& str, std::size_t* pos = nullptr);
long double stold(const std::string& str, std::size_t* pos = nullptr);

std::string to_string(int value);   // and long, long long, unsigned..., float, double, long double
Name Description
str The text to parse (a std::wstring overload set exists too).
pos If not null, receives the index of the first unconverted character.
base 2 to 36, or 0 to auto-detect (0x hex, leading 0 octal, otherwise decimal). Integer functions only.
value The number to format.

Returns: the parsed number, or the formatted string.

Errors: std::invalid_argument if no conversion could be made (no digits at the start); std::out_of_range if the value does not fit the type. stoul("-1") does not throw: it wraps to the maximum value.

Usage notes

  • Partial parsing is silent. stoi("12abc") is 12. Pass &pos and compare it with size() when the whole input must be a number.
  • Cost of exceptions. Input validation by try/catch is slow when many inputs are bad; use from_chars or strtol there.
  • to_string of floating-point uses the %f format: six digits after the point, no exponent, no round-trip guarantee. For control use std::format ("{:.2f}") or to_chars.
  • Locale. stod and to_string(double) use the current C locale for the decimal point; the default "C" locale means ..
  • char is not special. std::to_string('A') converts the integer 65 to "65".
  • Hex and binary output are not supported by to_string; use std::format("{:x}", n) or std::bitset.
  • Whitespace and signs. A leading + or - is accepted; leading whitespace is skipped; trailing whitespace is left unconverted.
  • stoi returns int and throws for values outside its range even if long would hold them.