How to Convert a Float to a string
-
Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks
-
Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks
itoa is only between int and char. if you want float or double try dtostr / strtod And still better, if you are using CString, then use CString::Format CString szValue = ""; szValue.Format (_T("%f"), doubleVariable); This works with hexadecimal, int, binary... take a look in documentation.
Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you ;)
-
Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks
CString t; t.Format("%f", floatVal);
-
Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks
the result from google search "c++ float to string" give this example...
int main() { char* str = new char[30]; float flt = 2.4567F; sprintf(str, "%.4g", flt ); cout< [Insert Witty Sig Here]
-
Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks
Besides all those Old-School C-Ways of converting, there is the std::stringstream:
#include <strstream>
#include <iostream>const double pi = 3.14; //Feel free to a digits at will
std::string piString;int main(int argc, char * argv[])
{
std::stringstream stream;
stream << pi;
//put any number of formatters here
stream >> piString;std::cout << piString;
return system("pause");
}Also, boost::lexical_cast[^] comes to mind.
Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
George Orwell, "Keep the Aspidistra Flying", Opening words -
Hi i hav converted an integer to a string using the following method, CString str; int i; char TempBuffer[50]; _itoa(i,TempBuffer,10) str=TempBuffer; Now how can i convert a Float to a string . any can please tell mi the solution(easy if the soln is similar to the above) thanks
saravananmechanical wrote:
char TempBuffer[50];
Why are you using an intermediary variable? Convert
i
directly intostr
.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
the result from google search "c++ float to string" give this example...
int main() { char* str = new char[30]; float flt = 2.4567F; sprintf(str, "%.4g", flt ); cout< [Insert Witty Sig Here]
Please tell us the site where you got that from so that it can be blacklisted! Or at least the original author so other developers here can watch out for their name on a resume... It is stupid enough to use
new
to allocate 30 bytes that are never used beyond the scope of allocation. It is also pretty dumb not todelete[]
them. It is beyond my understanding why someone would make such code public. I mean, come on, at least show stuff like this to your friends first... Oh, and the%g
specifier is technically fordouble
s, notfloat
s. :) And mixes C and C++ style output (could format using the stream), etc... :suss: Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Please tell us the site where you got that from so that it can be blacklisted! Or at least the original author so other developers here can watch out for their name on a resume... It is stupid enough to use
new
to allocate 30 bytes that are never used beyond the scope of allocation. It is also pretty dumb not todelete[]
them. It is beyond my understanding why someone would make such code public. I mean, come on, at least show stuff like this to your friends first... Oh, and the%g
specifier is technically fordouble
s, notfloat
s. :) And mixes C and C++ style output (could format using the stream), etc... :suss: Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFileshttp://www.thescripts.com/forum/thread550915.html
[Insert Witty Sig Here]
-
http://www.thescripts.com/forum/thread550915.html
[Insert Witty Sig Here]
You just as easily could have taken Google's second or third result and been much better off.
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
the result from google search "c++ float to string" give this example...
int main() { char* str = new char[30]; float flt = 2.4567F; sprintf(str, "%.4g", flt ); cout< [Insert Witty Sig Here]
VonHagNDaz wrote:
[Insert Witty Sig Here]
waiting for something to insert [:)]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You