0
0
CppHow-ToBeginner · 2 min read

C++ How to Convert String to Float with Examples

In C++, you can convert a string to a float using the function std::stof(yourString), which returns the floating-point number represented by the string.
📋

Examples

Input"3.14"
Output3.14
Input"0.001"
Output0.001
Input"-123.456"
Output-123.456
🧠

How to Think About It

To convert a string to a float, think of the string as a number written in text form. You want to read that text and get the actual number it represents. The standard library provides a simple function that reads the string and returns the float value, handling decimal points and signs automatically.
📐

Algorithm

1
Take the input string that represents a number.
2
Use the standard function to read the string and convert it to a float value.
3
Return or use the float value as needed.
💻

Code

cpp
#include <iostream>
#include <string>

int main() {
    std::string str = "3.14";
    float num = std::stof(str);
    std::cout << num << std::endl;
    return 0;
}
Output
3.14
🔍

Dry Run

Let's trace converting the string "3.14" to a float using std::stof.

1

Input string

str = "3.14"

2

Convert string to float

num = std::stof(str) -> 3.14

3

Print float value

Output: 3.14

StepStringFloat Value
1"3.14"N/A
2"3.14"3.14
3N/A3.14
💡

Why This Works

Step 1: Reading the string

The function std::stof reads the characters in the string and understands the number format including decimal points.

Step 2: Converting to float

std::stof converts the string characters into a floating-point number stored in a variable of type float.

Step 3: Using the float

After conversion, you can use the float value in calculations or print it as a number.

🔄

Alternative Approaches

Using std::stringstream
cpp
#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string str = "3.14";
    std::stringstream ss(str);
    float num;
    ss >> num;
    std::cout << num << std::endl;
    return 0;
}
This method is more flexible for complex parsing but slightly longer and less direct than std::stof.
Using atof from <cstdlib>
cpp
#include <iostream>
#include <cstdlib>

int main() {
    const char* cstr = "3.14";
    float num = std::atof(cstr);
    std::cout << num << std::endl;
    return 0;
}
This is a C-style approach that works but does not handle errors well compared to std::stof.

Complexity: O(n) time, O(1) space

Time Complexity

The conversion scans each character of the string once, so it takes linear time relative to the string length.

Space Complexity

The function uses a fixed amount of extra memory, so space complexity is constant.

Which Approach is Fastest?

std::stof is generally faster and safer than std::stringstream and atof, which are older or more flexible but slower.

ApproachTimeSpaceBest For
std::stofO(n)O(1)Simple, safe conversion
std::stringstreamO(n)O(1)Complex parsing scenarios
atofO(n)O(1)Legacy C-style code, less safe
💡
Use std::stof for simple and safe string to float conversion in modern C++.
⚠️
Trying to convert strings with invalid characters without error handling causes exceptions or wrong results.