Parses string interpreting its content as a floating-point number, which is returned as a value of type float.
Syntax :
float stof (const string& str, size_t* idx = 0);
float stof (const wstring& str, size_t* idx = 0);
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.
Below is the C++ implementation of std::stof :
#include <bits/stdc++.h>
int main()
{
std::string str = "100.80" ;
float val = std::stof(str);
std::cout << val;
return 0;
}
|
Output:
100.8
#include <bits/stdc++.h>
int main()
{
std::string str = "1000" ;
float val = std::stof(str);
std::cout << val;
return 0;
}
|
Output:
1000
Last Updated :
02 Aug, 2017
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...