How to Convert String to Float/Double in C++

by | C++, Programming, Tips

This tutorial will go through how to convert a String to a float/double and vice versa.


Why Convert Strings to Float/Double?

You have to convert string literals holding numerical values to numbers to perform mathematical operations.

We want to add two numerical strings using the addition operator in the following example.

#include <iostream>
#include<string>

int main() {

    std::string str1 = "5";

    std::string str2 = "10";

    std::string str3 = str1 + str2;

    std::cout << " 5 + 10  = " << str3 << std::endl;

    return 0;

}

We expect 5 + 10 to equal 15, but we will concatenate the strings together:

 5 + 10  = 510

Let’s go through how to convert the two numerical strings to add them together.

Strings in C++

A C++ string variable contains a collection of characters surrounded by double-quotes. There are two types of strings used in C++.

  • C-strings, which are arrays of type char terminated by the null character \0.
  • Strings that are objects of the Standard C++ Library string class.

Convert String to Float in C++

The function stof parses a string, interprets it as a floating-point number and returns it as a value of type float. Let’s look at the example below:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str1 = "5";
    string str2 = "10";

    // convert string to float
    float num1 = stof(str1);
    float num2 = stof(str2);
    sum = num1 + num2
    cout<< "5 + 10  = " << sum << endl;

    return 0;
}

Let’s run the code to see the result:

5 + 10  = 15

Convert String to Double in C++

The function stod parses a string, interprets it as a double and returns it as a value of type double. Let’s look at the example below:

#include <iostream>
#include <string>

using namespace std;

int main() {
    string str1 = "5";
    string str2 = "10";

    // convert string to double
    double num1 = stod(str1);
    double num2 = stod(str2);
    double sum = num1 + num2;

    cout<< "5 + 10  = " << sum << endl;

    return 0;
}

Let’s run the code to see the result:

5 + 10  = 15

Convert Char Array to Float in C++

We can use the stof function to convert a numerical char array to a floating-point number. Let’s look at the example below:

#include <iostream>

using namespace std;

int main() {

    char arr1[] = "5";
    char arr2[] = "10";

    float num1 = stof(arr1);
    float num2 = stof(arr2);

    float sum = num1 + num2;

    cout << "5 + 10 = " << sum << endl;
    
    return 0;

}

Let’s run the code to see the result:

5 + 10 = 15

Convert Char Array to Double in C++

We can use the stod function to convert a numerical char array to a double number. Let’s look at the example below:

#include <iostream>

int main() {
    char arr[] = "245.789""";

    float num_double = std::atof(arr);

    std::cout << "num_double = " << num_double << std::endl;

    return 0;
}

Let’s run the code to see the result:

num_double = 245.789

Convert Float to String in C++ Using to_string()

We can use the C++11 to_string() function to convert a floating-point number to a string. Let’s look at the example below:

#include <iostream>
#include <string>

int main() {
    float num_float = 245.789F;

    std::string str1 = std::to_string(num_float);

    std::cout << "Float to String = " << str1 << std::endl;

    return 0;
}

Let’s run the code to see the result:

Float to String = 245.789001

Convert Double to String in C++ Using to_string()

We can use the C++11 to_string() function to convert a double number to a string. Let’s look at the example below:

#include <iostream>
#include <string>

int main() {
    float num_float = 245.789F;

    std::string str1 = std::to_string(num_float);

    std::cout << "Float to String = " << str1 << std::endl;

    return 0;
}

Let’s run the code to see the result:

Double to String = 245.789000

Convert Float to String in C++ Using stringstream

For C++ compilers older than C++ 11, we can the stringstream objects. A stringstream object uses a string buffer to store a sequence of characters. Let’s look at the examples below:

#include <iostream>
#include<string>
#include<sstream> 

int main() {
    float num_float = 245.2743F;


    // stringstream object
    std::stringstream ss;

    // adding char array to buffer
    ss << num_float;

    // empty the buffer into a string
    std::string str = ss.str();

    std::cout << "Float to String = " << str << std::endl;

    return 0;
}
Float to String = 245.274

Convert Double to String in C++ Using stringstream

#include <iostream>
#include<string>
#include<sstream>

int main() {
    double num_double = 245.2743;

    std::stringstream ss;

    ss << num_double;


    std::string str = ss.str();

    std::cout << "Double to String = " << str << std::endl;

    return 0;
}
Double to String = 245.274

Summary

Congratulations on reading to the end of this tutorial. We have gone through how to convert a string to a float or double and vice-versa.

For further reading on string manipulation, go to the articles:

Have fun and happy researching!

Profile Picture
Senior Advisor, Data Science | [email protected] | + posts

Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.

Buy Me a Coffee ✨