How to convert a string to a signed float? [duplicate] - c++

This question already has answers here:
std::string to float or double
(16 answers)
Closed 8 years ago.
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.

strtod() is a good bet for C.
I have no idea if C++ has other bets.

The std::stof function should work fine.

You can use istringstream:
std::string num = "-0.25";
std::istringstream iss ( num);
float f_val = 0;
iss >> f_val;

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;

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/

Related

String to long double conversion in c++98

Anyone know how to convert a string to long double? For example lets say we have
string S = "3.236568949"
I want to take what inside the string and convert it to a long double variable x.
Note: In c++11 their exist stold function do so but I'm using c++98 not c++11.
Also I don't need to cout the string with certain precision because I know setprecision function already but what I want is just to convert to long double. In other wards,I need x to equal the exact number inside the string and not an approximated version of the number. Finally I want to tell you that my string contains a valid number.
you can use the C function :
#include <stdlib.h>
int main(){
char* convertme="3.236568949";
double converted=atof(convertme);
return 0;
}
You can use a std::stringstream to convert the string into a long double. The std::stringstream acts just like cin but instead of getting the input from the user you load it into the stream with a std::string. That looks like
std::string data = "3.236568949"
std::stringstream ss(data);
long double ld;
ss >> ld;
Do note that when dealing with floating point numbers you are going to have to deal with imprecision. For more on this see Is floating point math broken?
You can use strtold() function from C99:
long double d = strtold( str.c_str(), nullptr );
to convert std::string to long double. Though floating point representation is approximate so nobody would guarantee that value after conversion is exactly the same is in your string.

Boost Lexical conversion (negative string ----> float) [duplicate]

This question already has answers here:
std::string to float or double
(16 answers)
Closed 9 years ago.
What would this return?
boost::lexical_cast<float>("-2");
I am not able to find a lexical_cast conversion from string to float example in the documentation.
Thanks
This:
float value = boost::lexical_cast<float>("-2");
Is basically equivalent to this:
float value;
{
std::stringstream ss;
ss << "-2";
ss >> value;
}
Of course, Boost's lexical_cast does a few other things behind the scenes, and handles errors with exceptions rather than iostream error states, but for the most part, if a conversion through a std::stringstring will work, boost::lexical_cast will work the same way.
The value of the float is, of course, -2.0f.

Which function should be use to converting string to long double?

Note that in general, double is different from long double.
strtod converts string to double, but which function should be use to converting string to long double?
In C++03, use boost::lexical_cast, or:
std::stringstream ss(the_string);
long double ld;
if (ss >> ld) {
// it worked
}
In C99, use strtold.
In C89, use sscanf with %Lg.
In C++11 use stold.
There may be subtle differences as to exactly which formats each one accepts, so check the details first...
You've tagged your question as "C++", so I'm going to give you a C++ answer:
Why not just use streams?
std::stringstream ss(myString);
long double x;
ss >> x;
In c++, I can only recommend boost::lexical_cast (or in general via the IOStreams).
In c ? no idea.
You can use istream to read long double from string. See here http://www.cplusplus.com/reference/iostream/istream/operator%3E%3E/
If you like scanf family of functions, read with %Lf

convert float to string [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert double to string C++?
I searched in the page but i haven't found the solution.
I have a method to convert from int to string. But now i need to convert from float/double to string. Because i want to write some data in a file.
Anyone could help me ??
Thanks in advance.
http://www.daniweb.com/software-development/cpp/threads/146718
#include <sstream>
std::string Convert (float number){
std::ostringstream buff;
buff<<number;
return buff.str();
}
You could use sprintf.
You could use stringstream with << operator.
See also Convert double to string C++?, How do I convert a double into a string in C++?, Convert double to string using boost::lexical_cast in C++?, Converting Double to String in C++, etc.
Can you not use the standard (C) function sprintf/fprintf etc?
This can help you : Convert double to string C++?
Is your file written using IOStreams? If so, just do this:
stream << number;
If not, and you really need a string, you can use an ostringstream for this. Boost's lexical_cast wraps the string streams in an easy-to-use fashion.

Convert a no. from string to integer [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert string to integer in c++
Convert a no. from string to integer.
Eg: str:"1234"
convert to int a=1234;
i want the hint with proper explaination
use atoi():
int foo = atoi(str.c_str());
If you want to do this properly and test for errors in conversion, I recommend the use of boost::lexical_cast. Here is an example of usage:
#include <boost/lexical_cast>
std::string num_string("1234");
try
{
int num=boost::lexical_cast<int>(numString);
}
catch (boost::bad_lexical_cast &ex)
{
// Handle failed conversions
}
If for whatever reason you cannot use boost in your project, at least use a standard stringstream to do the conversion, in order to get some semblance of error checking.
Since you tagged it C++ you could have a look at istringstream:
bool convertStr(const char *str, unsigned int *num) {
istringstream iss(str);
return (iss >> *num);
}
Or you could even use templates:
template <class T>
bool fromString(T &t, const string &s,
ios_base& (*f)(ios_base&) = dec) {
istringstream iss(s);
return !(iss >> f >> t).fail();
}
The technique that this task is probably trying to get at is accessing each of the digits, e.g., str[0] - '0' = 1 and multiply by their place values. But atoi is much faster!