Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Convert String to Number and Vice Versa in C++



In this article, we will see how to convert a string to a number and a number to string using modern C++ techniques.

Understanding Strings and Numbers

In C++, strings and numbers are two different data types. A string is a sequence of characters enclosed in double quotes, and a number can be any numerical value such as integer, float, double, etc. Strings are used to represent text data and numbers are used for mathematical calculations. Let's see an example of defining a string and number.

// Define a string
string str = "Hello World";

// Define an integer number
int num = 100;

// Define a decimal number
float decimalNum = 99.99;

String to Number Conversion

To convert a string to a number, C++ provides several inbuilt functions defined in the <string> header. There are different functions if you want to convert a string to integer or a string to float number. The section below lists all the functions available for converting strings to numbers.

  • stoi(): This function will convert a string to an integer. It can also take a second argument (optional) to specify the position in the string where the conversion should start.
  • stol(): This function will convert an input string to a long integer. Similar to stoi(), it can also take a second argument to specify the position in the string where the conversion should start.
  • stoll(): This function will take a string as input and convert it to a long long integer.
  • stof(): This function will take an input string and convert it to a float type.
  • stod(): This function will take a string as input and convert it to a double type.

Example

In the example code below, we have used the stoi() and stof() functions to convert a string to an integer and a float respectively.

#include <iostream>
#include <string>
using namespace std;

int main() {
    // Declaring string variables and initializing them
    string strInt = "123";
    string strFloat = "45.67";

    // Using functions to convzert the strings to numbers
    int numInt = stoi(strInt);
    float numFloat = stof(strFloat);

    // Displaying the converted numbers
    cout << "Integer: " << numInt << endl;
    cout << "Float: " << numFloat << endl;

    return 0;
}

The output of the above code will be:

Integer: 123
Float: 45.67

Number to String Conversion

Theto_string() function is used to convert any number to string data type. This function is also defined in the <string> header of C++.

  • to_string(int): This declaration will take an integer as argument and convert it to a string type.
  • to_string(long): This declaration will take a long integer and convert it to a string type.
  • to_string(long long): This declaration will take a long long integer as input parameter and convert it to a string type.
  • to_string(float): This declaration will take a float data type as input and convert it to a string type.
  • to_string(double): This declaration will take a double data type as input argument and convert it to a string type.

Example

In the example code below, we have used the to_string() function with different parameters to convert an integer and a double to their string representation.

#include <iostream>
#include <string>
using namespace std;

int main() {
    // Declaring number variables and initializing them
    int numInt = 100;
    double numDouble = 99.99;

    // Using functions to convert the numbers to strings
    string strFromInt = to_string(numInt);
    string strFromDouble = to_string(numDouble);
    
    // Displaying the converted strings
    cout << "String from int: " << strFromInt << endl;
    cout << "String from double: " << strFromDouble << endl;

    return 0;
}

The output of the above code will be:

String from int: 100
String from double: 99.990000
Updated on: 2025-05-27T18:10:07+05:30

756 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements