Loading...
C++ String to float and double Conversion

C++ String to float and double Conversion

In this tutorial, we will learn how to convert string to floating-point numbers and vice versa with the help of examples.


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:
1) std::stof() - It convert string into float
Syntax:

float stof( const string& str, size_t* pos = 0 );
float stof( const wstring& str, size_t* pos = 0 );
Parameters
str : the string to convert
pos : address of an integer to store the number of characters processed
This parameter can also be a null pointer, in which case it is not used.
Return value: it returns value of type float.


2) std::stod() - It convert string into double
Syntax:

double stod( const std::string& str, std::size_t* pos = 0 );
double stod( const std::wstring& str, std::size_t* pos = 0 );
Return Value: return a value of type double
Parameters
str : the string to convert
pos : address of an integer to store the 
number of characters processed. This parameter can also be 
a null pointer, in which case it is not used.


3) std::stold() - It convert string into long double.
Syntax:

long double stold( const string& str, size_t *pos = 0 );
long double stold (const wstring& str, size_t* pos = 0);
Parameters : 
str : the string to convert
pos : address of integer to store the index of the first unconverted character.
This parameter can also be a null pointer, in which case it is not used.
Return value : it returns value of type 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 = "251.2567";

    // 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 = 251.257
num_double = 251.257

Example 2: C++ char Array to double

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

For Example :-

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

int main() {

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

    double num_double = std::atof(str);

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

Output

num_double = 251.257

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()

This function accepts a number (can be any data type) and returns the number in the desired string.

#include <iostream>
#include <string>

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

    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 = 251.256703
Double to String = 251.256700

Example 4: float and double to string Using stringstream

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

int main() {
    float num_float = 251.2567F;
    double num_double = 251.2567;
        
    // 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 = 251.257
Double to String = 251.257

Recommended Tutorials:
To know about converting a string to int, visit C++ string to int and Vice-versa


We hope that this tutorial helped you develop better understanding of the concept of String to float/double in C++.

Keep Learning : )


- Related Topics