C++ String to float/double and vice-versa

C++ string to float and double Conversion

The easiest way to convert a string to a floating-point number is by using these C++11 functions:

  • std::stof() - convert string to float
  • std::stod() - convert string to double
  • std::stold() - convert string to long double.

These functions are defined in the string header file.


Example 1: C++ string to float and double

#include <iostream>
#include <string>

int main() {
    std::string str = "123.4567";

    // convert string to float
    float num_float = std::stof(str);

    // convert string to double
    double num_double = std::stod(str);

   std:: cout<< "num_float = " << num_float << std::endl;
   std:: cout<< "num_double = " << num_double << std::endl;

    return 0;
}

Output

num_float = 123.457
num_double = 123.457

Example 2: C++ char Array to double

We can convert a char array to double by using the std::atof() function.

#include <iostream>

// cstdlib is needed for atoi()
#include <cstdlib>

int main() {

    // declaring and initializing character array
    char str[] = "123.4567";

    double num_double = std::atof(str);

    std::cout << "num_double = " << num_double << std::endl;
    
    return 0;
}

Output

num_double = 123.457

C++ float and double to string Conversion

We can convert float and double to string using the C++11 std::to_string() function. For the older C++ compilers, we can use std::stringstream objects.


Example 3: float and double to string Using to_string()

#include <iostream>
#include <string>

int main() {
    float num_float = 123.4567F;
    double num_double = 123.4567;

    std::string str1 = std::to_string(num_float);
    std::string str2 = std::to_string(num_double);

   std::cout << "Float to String = " << str1 << std::endl;
   std::cout << "Double to String = " << str2 << std::endl;

    return 0;
}

Output

Float to String = 123.456703
Double to String = 123.456700

Example 4: float and double to string Using stringstream

#include <iostream>
#include<string>
#include<sstream> // for using stringstream

int main() {
    float num_float = 123.4567F;
    double num_double = 123.4567;
  
    // creating stringstream objects
    std::stringstream ss1;
    std::stringstream ss2;
  
    // assigning the value of num_float to ss1
    ss1 << num_float;
  
    // assigning the value of num_float to ss2
    ss2 << num_double;

    // initializing two string variables with the values of ss1 and ss2
    // and converting it to string format with str() function
    std::string str1 = ss1.str();
    std::string str2 = ss2.str();
  
    std::cout << "Float to String = " << str1 << std::endl;
    std::cout << "Double to String = " << str2 << std::endl;

    return 0;
}

Output

Float to String = 123.457
Double to String = 123.457

Recommended Reading: C++ string to int.

Did you find this article helpful?