like :
string num = "-0.25";
how can I convert it to a signed float?
C++11: std::stof(num)
to convert to float
; also stod
for double
and stold
for long double
.
Historically: std::strtod
(or std::atof
if you don't need to check for errors); or string streams.
You can use istringstream
:
std::string num = "-0.25";
std::istringstream iss ( num);
float f_val = 0;
iss >> f_val;
You can use the atof
function.
http://www.cplusplus.com/reference/cstdlib/atof/
For C++ you can also use std::stof
http://www.cplusplus.com/reference/string/stof/
You can convert the string to a signed float by using the function atof. Like :
float myValue = (float)atof("0.75");
Note that you should also checked if the passed value is a valid numerical value otherwise the behaviour could be unpredictable.
There is also an other solution :
string mystr ("1204");
int myint;
stringstream(mystr) >> myint;