How to Convert Float to String in C++

  1. Use Macro Expression to Convert a Float to a String in C++
  2. Use the std::to_string() Method to Convert a Float to a String in C++
  3. Use std::stringstream Class and str() Method to Convert a Float to a String in C++

This article introduces several methods of converting a float value to a string in C++.

Use Macro Expression to Convert a Float to a String in C++

Preprocessor macros can be used to convert a constant float number to a string value. One thing to bear in mind is that this solution only applies to literal floating-point number conversion. The following code example shows how to call the string variable constructor statement with the stringizing macro. STRING macro uses the # operator to convert passed arguments to a string constant.

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

#define STRING(num) #num

int main() {
    string num_str(STRING(123.456));
    num_str.empty() ? cout << "empty\n" :
                        cout << num_str << endl;

    return EXIT_SUCCESS;
}

Output:

123.456

In case the floating-point constant is defined as another macro expression, the above code should be substituted with a two-level macro to yield correct results as shown in the next code segment:

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

#define STRING(num) STR(num)
#define STR(num) #num

#define NUMBER 123.456

int main() {
    string num_str(STRING(NUMBER));
    num_str.empty() ? cout << "empty\n" :
                       cout << num_str << endl;


    return EXIT_SUCCESS;
}

Output:

123.456

Use the std::to_string() Method to Convert a Float to a String in C++

The to_string function is defined in <string> header and can convert various numeric types to a string value. The method takes a numeric value as a parameter and returns std::string value. Note that, to_string may return unexpected results as the number of significant digits in the returned string can be zero as demonstrated in the example code:

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main() {

    float n1 = 123.456;
    double n2 = 0.456;
    double n3 = 1e-40;
    string num_str1(std::to_string(n1));
    string num_str2(std::to_string(n2));
    string num_str3(std::to_string(n3));
    num_str1.empty() ? cout << "empty\n" : cout << num_str1 << endl;
    num_str2.empty() ? cout << "empty\n" : cout << num_str2 << endl;
    num_str3.empty() ? cout << "empty\n" : cout << num_str3 << endl;

    return EXIT_SUCCESS;
}

Output:

123.456001
0.456000
0.000000

Use std::stringstream Class and str() Method to Convert a Float to a String in C++

std::stringstream is a powerful class for conducting input/output operations on string objects. We utilize it to insert a single floating-point variable into the stringstream object in this solution. Then we call the str builtin method to process numeric type to string object. Note that the returned string is a temporary object, so calling other methods on the result will yield a dangling pointer (see the line that’s commented out in the example).

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

using std::cout; using std::cin;
using std::endl; using std::string;

int main() {
    float n1 = 123.456;
    std::stringstream sstream;

    sstream << n1;
    string num_str = sstream.str();
    //auto *ptr = sstream.str().c_str(); // RESULTS in dangling pointer
    num_str.empty() ? cout << "empty\n" : cout << num_str << endl;

    return EXIT_SUCCESS;
}

Output:

123.456

Related Article - C++ Float

  • How to Generate a Random Float Number in C++