Open In App

C++ String to Float/Double and Vice-Versa

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. std::stof() – convert string to float
  2. std::stod() – convert string to double
  3. std::atof() – convert a char array to double
  4. 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;
}


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




// 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;
}


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




// 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;
}


Output

Float to String = 678.123
Double to String = 678.123


Last Updated : 27 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads