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.
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 12 years ago.
Possible Duplicate:
C++ concatenate string and int
Hi,
In C# I can write like this:
int i = 0;
string text = "out.jpg";
while(true)
{
i++;
Object.write(i+text, stream);
}
But this is not true for C++. the problem is at: i + default.
How could I fix this in C++?
Thanks in advance. Your help is much appreciated!
You could use a stringstream...
std::stringstream ss;
ss << i << text;
Object.write(ss.str(), stream);
default is a keyword in C++. You can't have string default in C++. And I don't see what you are trying to achieve. Please clarify
take a look at stringstreams or boost.format http://www.boost.org/doc/libs/1_38_0/libs/format/doc/format.html
boost::format("%1%%2%") % i % default_;
default is a reserved keyword. Change the variable name to defaultStr or similar, and everything should work fine.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to parse a string to an int in C++?
How do you convert a C++ string to an int?
Assume you are expecting the string to have actual numbers in it ("1", "345", "38944", for example).
Also, let's assume you don't have boost, and you really want to do it the C++ way, not the crufty old C way.
#include <sstream>
// st is input string
int result;
stringstream(st) >> result;
Use the C++ streams.
std::string plop("123");
std::stringstream str(plop);
int x;
str >> x;
/* Lets not forget to error checking */
if (!str)
{
// The conversion failed.
// Need to do something here.
// Maybe throw an exception
}
PS. This basic principle is how the boost library lexical_cast<> works.
My favorite method is the boost lexical_cast<>
#include <boost/lexical_cast.hpp>
int x = boost::lexical_cast<int>("123");
It provides a method to convert between a string and number formats and back again. Underneath it uses a string stream so anything that can be marshaled into a stream and then un-marshaled from a stream (Take a look at the >> and << operators).
I have used something like the following in C++ code before:
#include <sstream>
int main()
{
char* str = "1234";
std::stringstream s_str( str );
int i;
s_str >> i;
}
C++ FAQ Lite
[39.2] How do I convert a std::string to a number?
https://isocpp.org/wiki/faq/misc-technical-issues#convert-string-to-num
Let me add my vote for boost::lexical_cast
#include <boost/lexical_cast.hpp>
int val = boost::lexical_cast<int>(strval) ;
It throws bad_lexical_cast on error.
Use atoi
Perhaps I am misunderstanding the question, by why exactly would you not want to use atoi? I see no point in reinventing the wheel.
Am I just missing the point here?
in "stdapi.h"
StrToInt
This function tells you the result, and how many characters participated in the conversion.