How to use Macros In C++
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
Convert Float to String In C++
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
Contact Us