0Pricing
C++ Academy · Lesson

String Conversions: to_string and stoi

Convert between strings and numeric types safely with std::to_string, std::stoi and std::stod.

Why String Conversions Matter

You constantly need to convert between numbers and text — for user input, configuration files, JSON, URLs, and logs.

std::to_string

std::to_string converts any built-in numeric type to a string. It uses default formatting.

#include <string>

std::string a = std::to_string(42);     // "42"
std::string b = std::to_string(-7);     // "-7"
std::string c = std::to_string(3.14);   // "3.140000"

All lessons in this course

  1. std::string vs C-Style Char Arrays
  2. String Operations: concat substr find replace
  3. String Stream stringstream for Parsing
  4. String Conversions: to_string and stoi
← Back to C++ Academy