Javatpoint Logo
Javatpoint Logo

std::stof in C++

Programming in a variety of fields, including systems programming, game development, and all in between, C++ is a strong and adaptable language. C++ has a number of functions for converting texts to numeric values and vice versa in order to handle numerical data effectively. The ability of std::stof to transform a string into a floating-point number makes it stand out among these functions.

What is std::stof?

C++11 standard introduced the C++ Standard Library function std::stof. It is mostly used to translate a string representing a floating-point number into its numerical equivalent, which is of type float. It is a component of the <string> header. It's especially helpful when you need to parse user input, configuration files, or data from outside sources.

Function Signature:

Let us dissect the parameters:

  • The input string you wish to transform is called str.
  • A size_t is pointed to by idx. Where the conversion stopped is determined by this optional parameter. The function will not return this information if idx is left blank or set to nullptr.

Example:

Let's take an example to illustrate the use of std::stof function in C++.

Output:

std::stof in C++

Dealing with Errors in std::stof:-

Although std::stof is a useful tool for parsing floating-point numbers, it's important to be aware of certain problems that could occur during conversion.

The following situations should be anticipated when working with user input or data from outside sources:

Invalid Data:

When an input string represents an invalid floating-point number, std::stof will throw an exception of type std::invalid_argument.

  • The code will handle the exception in this scenario and output an error message to the console.

Overflow or Underflow:

Overflow or underflow problems may arise if the value in the input string is greater than the range that a float can hold. Std::stof will raise an exception of type std::out_of_range in these circumstances.

  • The string "1e40" is too long to be represented as a float, therefore this code throws an exception when it tries to convert it.
  • To effectively manage these circumstances, it's a good idea to wrap your standard::stof calls within try-catch blocks, as the aforementioned examples illustrate.

Handling Whitespace and Other Characters:

The purpose of std::stof is to translate strings that correspond to real floating-point integers. On the other hand, it can be quite tolerant of some extra characters inside the string as well as leading and trailing whitespace.

  • Here, the numeric value 42.42 will be successfully extracted from the string using std::stof, which will disregard the leading and trailing spaces.
  • In the same way, std::stof will deal with both positive and negative signs in the string:
  • The accurate numerical values 42.42 and -42.42 will be present in both posNumber and negNumber.
  • It's important to remember that other non-numeric characters, including commas and currency symbols, cannot be handled by std::stof. The conversion will fail and a std::invalid_argument exception will be thrown if these characters are used.

Example Program:

Let's take an example to illustrate the use of std::stof function in C++.

Output:

std::stof in C++





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA