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!!

Related Posts

  • Round to 2 decimal places in C++
    25 January

    Round to 2 decimal places in C++

    Table of ContentsRepresenting Decimal Numbers in C++Ways to round to 2 decimal places in C++Using the ceil() function to round to 2 decimal places in C++Using the floor() function to round to 2 decimal places in C++Using the int typecast to round to 2 decimal places in C++Using the std::setprecision() function to round to 2 […]

  • Fill array with random numbers c++
    19 January

    Fill Array With Random Numbers in C++

    Table of ContentsThe rand() Function in C++How to Fill the Array With Random Numbers in C++?How to Fill the Array With Random Numbers in a Range in C++?Conclusion IN this post, we will see how to fill array with random number in C++. Arrays are the most commonly used data structures in the programming domain. […]

  • Convert ASCII to char in C++
    15 January

    Convert ASCII to Char in C++

    Table of ContentsHow to Convert ASCII values to Char in C++?Convert ASCII to Char in C++ Using Char() FunctionConvert ASCII to Char in C++ Using C-Style CastConvert ASCII to Char in C++ Using the Assignment OperatorConvert ASCII to Character in C++ Using sprintf() FunctionConclusion The characters are represented using integers in most of the encoding […]

  • Check if input is Integer in C++
    13 January

    Check If Input Is Integer In C++

    Table of ContentsHow to Check Whether the Input Is an Integer in C++?Check Whether the Input Is an Integer Using the isdigit() Function in C++Check Whether the Input Is an Integer Using the Ascii Values in C++Conclusion C++ is a powerful and popular programming language and we use it widely in coding applications. It provides […]

  • Convert String to lowercase in C++
    11 January

    Convert String to lowercase in C++

    Table of ContentsWays to Convert a String to Lowercase in C++Using the tolower() and transform() functionUsing the for loopUsing the boost library In this post, we will see how to convert String to lowercase in C++. We can assume strings to be an array of characters. Each character has a specific ASCII value, and characters […]

  • Split String by comma in C++
    10 November

    Split String by comma in C++

    Table of ContentsUsing the for loopUsing the getline() functionUsing the strtok() functionUsing the std::string::erase() and std::string::find() function In this article, we will split a string by comma in C++. There are several methods available to achieve this. For this, we can create a function and use some inbuilt functions. Using the for loop In this […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.