C++ String to Float/Double and Vice-Versa
In this article, we will learn how to convert String To Float/Double And Vice-Versa. In order to do conversion we will be using the following C++ functions:
- std::stof() – convert string to float
- std::stod() – convert string to double
- std::atof() – convert a char array to double
- std::to_string – convert any data type number to string
Converting String to Float/Double
1. Using std::stof() and std::stod() function
stof() function takes a string object as an input and returns the floating point number corresponding to that string as an output. On the other hand stod() takes a string as input and returns double data type as an output.
Syntax:
float variable_name = std::stof(string_name);
double variable_name = std::stod(string_name);
Example:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str = "678.1234" ;
float str_float = std::stof(str);
double str_double = std::stod(str);
cout<< "string as float = " << str_float << endl;
cout<< "string as double = " << str_double << endl;
return 0;
}
|
Output
string as float = 678.123
string as double = 678.123
2. Using std::atof() function
In C++ the character array can be converted to any numeric data type like float/double using the help of atof() function. This function takes a string as a parameter and returns float/double as output.
Syntax:
float variable_name = std::atof(string_name);
double variable_name = std::atof(string_name);
Example:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
char str[] = "678.1234" ;
float str_float = std:: atof (str);
double str_double = std:: atof (str);
cout<< "char array as float = " << str_float << endl;
cout<< "char array as double = " << str_double << endl;
return 0;
}
|
Converting Float/Double to String
1. Using to_string() function
Using the std::to string() function in C++11, we can convert float and double data types to strings.
Syntax:
string variable_name = std::to_string(number);
Here parameter number can be of any data type like float/double.
Example:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
float number1 = 678.1234;
double number2 = 678.1234;
string float_str = std::to_string(number1);
string double_str = std::to_string(number2);
cout<< "Float to String = " << float_str << endl;
cout<< "Double to String = " << double_str << endl;
return 0;
}
|
Output
Float to String = 678.123413
Double to String = 678.123400
2. Using stringstream and str() function
In this approach, we create stringstream object with the help of std::stringstream in C++, and then this object is assigned a value. The value can be of any data type like float/double. Then this object is converted to a string using the str() function in C++.
Example:
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
float number1 = 678.1234;
double number2 = 678.1234;
std::stringstream ss1;
std::stringstream ss2;
ss1 << number1;
ss2 << number2;
string float_str = ss1.str();
string double_str = ss2.str();
cout<< "Float to String = " << float_str << endl;
cout<< "Double to String = " << double_str << endl;
return 0;
}
|
Output
Float to String = 678.123
Double to String = 678.123
Last Updated :
27 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...