Convert float to string in C++ (3 Ways)

In this article, we will learn about three different ways to convert a float to a string in C++ i.e.

  1. Using std::to_string() function
  2. Using std::stingstream
  3. Using Boost’s lexical_cast() function.

Let’s discuss them one by one.

Convert float to string using to_string()

C++ provides a function std::to_string() for data type conversion. We can use this to convert a float to a string. For example,

#include <iostream>
#include <string>

using namespace std;

int main()
{
    float number = 1024.671734;

    // Convert float to string
    string num_str = to_string(number);

    cout<<num_str<<endl;

    return 0;
}

Output:

1024.671753

We passed a float value to the to_string() function, and it converted the given float to a string and returned the string object. It will keep the precision level as it is. If you want more control over the precision level of float while converting it to string, then look at the following technique using stringstream.

Advertisements

Convert float to string with specified precision using stringstream in C++

C++ provides a class stringstream, which provides a stream-like functionality. We can insert different types of elements in stringstream and get a final string object from the stream. We can use that to convert float to string. In the stream, we can also set the precision level of float-type elements before conversion. Check out this example,

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

using namespace std;

int main()
{
    float number = 1024.671734;

    stringstream stream;  

    // Set precision level to 3
    stream.precision(3);
    stream << fixed;
    
    // Convert float to string
    stream<<number;  
    string str  = stream.str();  

    cout<<str<<endl;

    return 0;
}

Output:

1024.672

In the stringstream, we inserted a float to the stringstream, and then the stream converted the float to string. Also, as we set the precision level to 3, it rounded off the value to keep only three digits after the dot.

Convert float to string using boost::lexical_cast()

The C++ boost library, provides a template function for data type conversion. It helps to convert elements from one data type to another. We can use that to convert a float to string. For example,

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>

using namespace std;
using namespace boost;

int main()
{
    float number = 1024.671734;
    
    // Convert float to string using boost library
    string str = lexical_cast<string>(number);  
    
    cout<<str<<endl;

    return 0;
}

Output:

1024.67175

Summary:

We learned about three different ways to convert a float value to a string in C++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top