Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Convert String to Integer Vector in C++
Next article icon

Convert Float to String In C++

Last Updated : 27 Nov, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we learn how we can convert float to string in C++ using different methods: 

  • Using the to_string()
  • Using stringstream
  • Using Macros
  • Using lexical_cast from the boost library

1. Using to_string()

The to_string() method takes a single integer variable or other data type and converts it into a string.

Syntax: -

string to_string (float value);

Example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {     float x=5.5;       string resultant;       resultant=to_string(x);     cout << "Converted value from float to String using to_string() is : "<<resultant<<endl;     return 0; } 

Output
Converted value from float to String using to_string() is : 5.500000

Explanation: The to_string function converts the given float value into the string.

2. Using stringstream 

A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input. The basic methods are:

  • clear() - To clear the stream.
  • str() - To get and set a string object whose content is present in the stream. 
  • operator <<- Add a string to the stringstream object. 
  • operator >>- Read something from the stringstream object.

Example:

C++
#include <bits/stdc++.h> using namespace std;  int main() {            float x=5.5;       stringstream s;       s<<x; // appending the float value to the streamclass       string result=s.str(); //converting the float value to string     cout <<"Converted value from float to String using stringstream is : "<<result<<endl;     return 0; } 

Output
Converted value from float to String using stringstream is : 5.5

Explanation: The stringstream class converts the float value from a variable to a string. It is an inbuilt class present in the C++ Library.

3. Using Macros

This method solely applies to floating point conversion. The '#' is used by the STRING macro to convert the floating-point values to String.

Syntax: 

#define STRING(Value) #Value  string gfg(STRING(Float_value));

Example:

C++
#include <bits/stdc++.h> #include <string> using namespace std; //using macro to convert float to string #define STRING(Value) #Value  int main() {     string gfg(STRING(5.5));     if(gfg.empty()) cout << "The String is empty"<<endl ;     else cout << gfg << endl;      return EXIT_SUCCESS; } 

Output
5.5

4. Using lexical_cast 

Boost.LexicalCast which is defined in the Library "boost/lexical_cast.hpp" provides a cast operator, boost::lexical_cast, that can convert numbers from strings to numeric types like int or double and vice versa. boost::lexical_cast is an alternative to functions like std::stoi(), std::stod(), and std::to_string(), which were added to the standard library in C++11. 

Syntax:

float x= 5.5;  string res = lexical_cast<string>(x);

Example:

C++
#include "boost/lexical_cast.hpp" #include <bits/stdc++.h> using namespace std; using boost::lexical_cast; using boost::bad_lexical_cast; int main() {    // Float to string conversion   float x= 5.5;   string res = lexical_cast<string>(x);   cout << res << endl;   return 0; } 

Output:

5.5

Next Article
Convert String to Integer Vector in C++

R

raj2002
Improve
Article Tags :
  • Technical Scripter
  • C++ Programs
  • C++
  • Technical Scripter 2022
  • CPP Strings Programs
Practice Tags :
  • CPP

Similar Reads

  • How to Convert String to Date in C++?
    In C++, we generally store the date and time data in the form of the object of type std::tm. Sometimes, this data is stored inside a string object and we need to convert it into the tm type for further processing. In this article, we will learn how to convert a string to date in C++. Example: Input:
    2 min read
  • Convert char* to std::string in C++
    Strings are generally represented as an instance of std::string class in C++. But the language also supports the older C-Style representation where they are represented as array of characters (char* or char[]) terminated by null character '\0'. In this article, we will learn how to convert the char*
    3 min read
  • Convert String to Char Array in C++
    In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++. Examples Input: str = "geeksforgeeks"Outp
    4 min read
  • How to Convert a std::string to char* in C++?
    In C++, strings are the textual data that is represented in two ways: std::string which is an object, and char* is a pointer to a character. In this article, we will learn how to convert a std::string to char* in C++. Example Input:string myString = "GeeksforGeeks";Output:char * myString = "Geeksfor
    1 min read
  • Convert String to Integer Vector in C++
    Strings are sometimes used to represent the numerical data which needs to be converted back to integer, but sometimes we may need to convert it to vector of integers instead due to some problems such as too large integer. In this article, we will learn different way to convert a string to integer ve
    2 min read
  • How to Convert std::string to Lower Case?
    Converting string to lower case means all the alphabets present in the string should be in lower case. In this article, we will learn how to convert the std::string to lowercase in C++. Examples Input: s = "GEEKS for GEEKS"Output: geeks for geeksExplanation: All characters of s are converted to lowe
    3 min read
  • C++ Program For String to Long Conversion
    In this article, we will learn how to convert strings to long in C++. For this conversion, there are 3 ways as follows: Using stol()Using stoul()Using atol() Let's start by discussing each of these methods in detail. Example: Input: s1 = "20" s2 = "30" Output: s1 + s2 long: 50 1. Using stol() In C++
    3 min read
  • How to Convert wstring to int in C++?
    In C++, std::wstring is a type of string where each character is of a wide character type. These types of strings can be used to store the numerical strings which can then be converted to their corresponding type. In this article, we will see how to convert a wstring to an int in C++. Input: wstr =
    2 min read
  • String Concatenation in C++
    String concatenation refers to the process of combining two or more strings into a single string. Generally, one string is appended at the end of the other string. In this article, we will learn how to concatenate two strings in C++. The simplest method to concatenate two strings in C++ is by using
    3 min read
  • How to Convert wstring to double in C++
    In C++, std::wstring is a type of string where each character is of a wide character type. Converting wstring to a double in C++ can be a common requirement especially when dealing with the inputs that are stored as unicode characters. In this article, we will learn how to convert a wstring to a dou
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences