Convert float to String in C++

Convert float to String in C++

In this article we will discuss three different ways to convert float to string in C++.

Using ostringstream to convert float to string in C++

In this method we use 0stringstream to pass a float number to the buffer and then convert it into a string by using the str() method. setprecision(int n) function is also used with fixed to provide precision to floating numbers correct to decimal numbers passed as argument in the function. It is defined in the header.

Note: The string returned is only a temporary object and if other methods are called on the results, then it will yield a dangling pointer.

Code:

Output

After converting to string: 9.2300

Using to_string() function to convert float to string in C++

to_string() function can also be used to convert float to string and it is defined in the header. This function can be used to convert different numeric values to string data type. It is very easy to implement and it can take floating point numbers as a parameter and will return string type value.

Note: This function can return unexpected output because the number of significant digits in the string returned by it can be zero as well and it can be seen in code.

Code

Output

Enter the float number to convert to string: 58.25828
After Converting to String: 58.258282

Using Macro expression to convert float to string in C++

This method will work only for literal floating point numbers. In this method, preprocessor macros can be utilized to convert constant floating point number to string. In the coding part, we can see the right way to call the string variable constructor. We need #operator for STRING macro to convert float to string in C++.

Code:

Output

After converting to string: 9876.5412

If we define a constant floating point number as another macro expression, then we can use two-level macro to get the correct results.
Code:

Output

After converting to string: 9876.5412

Using lexical_cast method from the Boost libraries to convert float to String in C++

Boost library has a lexical_cast function which can be used to convert float to String.
Code:

Output

Enter the float number to convert to string: 23.228
After Converting to String: 23.228

Conclusion

In this article, we discussed three different ways to convert float to string in C++. In all the methods, we have to take care of a few things while using them.
Happy Learning!!

Was this post helpful?

Related Posts

  • 19 May

    Get Type of Object in C++

    Table of ContentsWhy Should We Determine the Type of Object in C++: Dynamic DispatchingGet Type of Object in C++ConclusionWas this post helpful? C++ is an object-oriented language where we often interact with objects of different types. The object interaction becomes more interesting when we add runtime polymorphism to it. The runtime polymorphism, also known as, […]

  • 19 May

    Convert Vector to Array in C++

    Table of ContentsVectors and Arrays in C++Convert Vector to Arrays in C++Using the for loop to convert vector to array in C++Using the copy() function to convert vector to array in C++Using the transform() function to convert vector to array in C++Using the data() function to convert vector to array in C++Using the & operator […]

  • How to initialize an array in Constructor in C++
    23 April

    How to initialize an Array in Constructor in C++

    Table of ContentsArraysConstructors of a ClassWays to initialize an array in Constructor in C++Initialize an array in Constructor With std::fill()Initialize an array in Constructor Using for LoopInitialize an array in Constructor Using Member Initializer ListConclusionWas this post helpful? This article discusses methods of initializing an array in constructor in C++. Let us first discuss arrays […]

  • Write String to file in C++
    31 March

    Write String to File in C++

    Table of ContentsUsing the fstream Library to write String to File in C++Using the Insertion Operator (<<) With the fstream ObjectUsing the write() Function of the fstream LibraryUsing the fwrite() Function to write String to File in C++ConclusionWas this post helpful? The files are used to store the data permanently on a disk. This mitigates […]

  • Print Array in C++
    23 March

    Print Array in C++

    Table of ContentsIntroduction of ArraysDifferent Methods to Print an Array in C++print array in C++ by Traversing Indicesprint array in C++ Using for_each() Functionprint array in C++ Using Range-Based for Loopprint array in C++ Using Iteratorsprint array Using ostream_iterator() Function and copy() Functionprint array Using C++17 copy() Function and make_ostream_joiner() FunctionConclusionWas this post helpful? An […]

  • Print Vector C++
    23 March

    Print Vector in C++

    Table of ContentsPrint Vector in C++ Using IndicesPrint Vector in C++ Using Range-Based for LoopPrint Vector in C++ Using for_each() FunctionPrint Vector in C++ Using IteratorPrint Vector in C++ by Overloading the Insertion Operator (<<)Print Vector in C++ Using copy() FunctionConclusionWas this post helpful? This article discusses the vector and how to print vector in […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.