C++ String to Float/Double and Vice-Versa
Last Updated :
28 Apr, 2025
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++
// C++ program to convert
// string into float/double
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Initializing the string
string str = "678.1234";
// Converting string to float
float str_float = std::stof(str);
// Converting string to double
double str_double = std::stod(str);
cout<< "string as float = " << str_float << endl;
cout<< "string as double = " << str_double << endl;
return 0;
}
Outputstring 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++
// C++ program to convert
// float/double into string
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Initializing the char array
char str[] = "678.1234";
// Converting char array to float
float str_float = std::atof(str);
// Converting char array to double
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++
// C++ program to convert
// float/double into string
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Initializing the numbers
float number1 = 678.1234;
double number2 = 678.1234;
// Converting float to string
string float_str = std::to_string(number1);
// Converting double to string
string double_str = std::to_string(number2);
cout<< "Float to String = " << float_str << endl;
cout<< "Double to String = " << double_str << endl;
return 0;
}
OutputFloat 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++
// C++ program to convert
// float/double into string
#include<bits/stdc++.h>
using namespace std;
int main()
{
// Initializing the numbers
float number1 = 678.1234;
double number2 = 678.1234;
// creating stringstream objects
std::stringstream ss1;
std::stringstream ss2;
// assigning values to
// stringstream objects
ss1 << number1;
ss2 << number2;
// Converting float to string
string float_str = ss1.str();
// Converting double to string
string double_str = ss2.str();
cout<< "Float to String = " << float_str << endl;
cout<< "Double to String = " << double_str << endl;
return 0;
}
OutputFloat to String = 678.123
Double to String = 678.123
Similar Reads
C++ Program to Find the Size of int, float, double and char In this article, we will learn to write a C++ program to find the size of int, float, double, and char. It is important to know the size of different data types especially when working with large datasets to optimize memory usage. The size of a variable can be determined using sizeof() operator in C
2 min read
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 stringstreamUsing MacrosUsing lexical_cast from the boost library1. Using to_string() The to_string() method takes a single integer variable or other data type and converts it into
3 min read
C++ Program For Double to String Conversion Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.76
2 min read
C++ Program For String to Double Conversion There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() Fun
3 min read
Abnormal behavior of floating point and double values Float is a 32 bit IEEE 754 single-precision Floating Point Number 1 bit for the sign, (8 bits for the exponent, and 23* for the value), i.e. float has 7 decimal digits of precision. Double is a 64 bit IEEE 754 double precision Floating Point Number (1 bit for the sign, 11 bits for the exponent, and
6 min read