A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/stof/ below:

function template

<string>

std::stof
float stof (const string&  str, size_t* idx = 0);float stof (const wstring& str, size_t* idx = 0);

Convert string to float

Parses str interpreting its content as a floating-point number, which is returned as a value of type float.

If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.

The function uses strtod (or wcstod) to perform the conversion (see strtod for more details on the process). Note that the format accepted by these functions depends on the current locale.



Parameters
str
String object with the representation of a floating-point number.
idx
Pointer to an object of type size_t, whose value is set by the function to position of the next character in str after the numerical value.
This parameter can also be a null pointer, in which case it is not used.

Return Value On success, the function returns the converted floating-point number as a value of type float.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// stof example
#include <iostream>   // std::cout
#include <string>     // std::string, std::stof

int main ()
{
  std::string orbits ("686.97 365.24");
  std::string::size_type sz;     // alias of size_t

  float mars = std::stof (orbits,&sz);
  float earth = std::stof (orbits.substr(sz));
  std::cout << "One martian year takes " << (mars/earth) << " Earth years.\n";
  return 0;
}

Possible output:
One martian year takes 1.88087 Earth years.


Complexity Unspecified, but generally linear in the number of characters interpreted.

Data races Modifies the value pointed by idx (if not zero).

Exceptions If no conversion could be performed, an invalid_argument exception is thrown.
If the value read is out of the range of representable values by a float (in some library implementations, this includes underflows), an out_of_range exception is thrown.

An invalid idx causes undefined behavior.



See also
stod
Convert string to double (function template)
stold
Convert string to long double (function template)
stoi
Convert string to integer (function template)
strtof
Convert string to float (function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4