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.
Related
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/
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Easiest way to convert int to string in C++
Does anyone know how to do that conversion?
I need to concatenate a intptr_t type to a string, and therefore need to convert it.
It's not an int, as It's a 64 bit OS.
Correct me if I'm wrong
Thanks
intptr_t is just a number. Therefore:
Easiest way to convert int to string in C++
...and others
Converting numbers to strings and strings to numbers
Simple:
std::to_string(ip);
Well, simple if you have C++11.
std::stringstream ss;
ss << ip;
ss.str();
(or if you prefer:
ss << std::hex << ip;
)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string
I want to convert integer to string, any one help me for these conversion?
itoa(*data->userid,buff1,10);
itoa(*data->userphone,buff2,10);
For C++, use std::stringstream instead.
#include <sstream>
//...
std::stringstream ss;
ss << *data->userid;
std::string userId = ss.str();
or std::to_string if you have access to a C++11 compiler.
If you have a C++11 compiler with the new std::to_string function you can use that. Otherwise use the std::stringstream solution by Luchian.
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.
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!