Convert a string to a float in C++
This post will discuss how to convert a string to a float in C++.
1. Using std::stof
Starting with C++11, we can use the std::stof function to parse a string as a floating-point number. Here is a simple, short, and elegant C++11 solution demonstrating this:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string s = "3.14"; float f = std::stof(s); std::cout << f << std::endl; // 3.14 return 0; } |
To get the returned value of type double, use the std::stod function instead:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string s = "3.14"; double d = std::stod(s); std::cout << d << std::endl; // 3.14 return 0; } |
A better option is to use the strtof() and strtod() functions, to get a floating-point number as a float or double, respectively. This function is fail-safe and never throws exceptions.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <cstdlib> int main() { std::string s = "3.14"; float f = strtof(s.c_str(), nullptr); std::cout << f << std::endl; // 3.14 double d = strtod(s.c_str(), nullptr); std::cout << d << std::endl; // 3.14 return 0; } |
2. Using sscanf() function
The sscanf() function can be used to read the formatted data from a string. The string is converted to the corresponding data type, as per the format specifier. To get a float value, use the %f format specifier. Similarly, use the %lf format specifier to get a double value.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <string> int main() { std::string s = "3.14"; float f; sscanf(s.c_str(), "%f", &f); std::cout << f << std::endl; // 3.14 double d; sscanf(s.c_str(), "%lf", &d); std::cout << d << std::endl; // 3.14 return 0; } |
3. Using std::istream::operator>>
The std::istringstream class is an input stream class to operate on strings. We can call the extraction operator (>>) to extract and parse characters from the input stream to the desired type value. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> #include <sstream> int main() { std::string s = "3.14"; float f; std::istringstream(s) >> f; std::cout << f << std::endl; // 3.14 double d; std::istringstream(s) >> d; std::cout << d << std::endl; // 3.14 return 0; } |
4. Using Boost
Alternatively, we can use the boost’s lexical_cast library for converting a string to float or double in C++. It internally uses streams.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <string> #include <boost/lexical_cast.hpp> int main() { std::string s = "3.14"; try { float f = boost::lexical_cast<float>(s.c_str()); double d = boost::lexical_cast<double>(s.c_str()); std::cout << f << std::endl; // 3.14 std::cout << d << std::endl; // 3.14 } catch( boost::bad_lexical_cast const& ) { // handle error } return 0; } |
5. Using std::from_chars
Since C++17, we can use std::from_chars function, defined in header <charconv>. The following code example shows invocation for this function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <charconv> int main() { std::string s = "3.14"; float f = 0.0; std::from_chars(s.data(), s.data() + s.size(), f); std::cout << f << std::endl; // 3.14 return 0; } |
The std::from_chars function does not throw any exception. However, we can use the enumeration std::errc defined in header <system_error> to check for any errors.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <iostream> #include <string> #include <charconv> #include <system_error> int main() { std::string s = "3.14"; float f = 0.0; auto [ptr, ec] = std::from_chars(s.data(), s.data() + s.size(), f); if (ec == std::errc{}) { std::cout << f << std::endl; // 3.14 } else if (ec == std::errc::invalid_argument) { std::cout << "String is not a number." << std::endl; } else if (ec == std::errc::result_out_of_range) { std::cout << "Number out of range" << std::endl; } return 0; } |
That’s all about converting a string to a float in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)