• Advertisement

Archived

This topic is now archived and is closed to further replies.

Char and Float (Simple)

This topic is 5094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would you go about converting a float to something you can use with a char variable. like i have: char *strTemp="Hello"; float aNumber = 2; now how would i get it to say: "Hello2" ?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Look up the itoa() and atoi() functions on Google.

You may need to cast the float to int first, or there may be a special function for floats, can someone enlighten us on that?

Share this post


Link to post
Share on other sites
sprintf(strTemp, "%s%f", strTemp, aNumber);

will result in strTemp containing "Hello2"

EDIT: Syncronous post with AP...

No, you use gcvt() for that.



(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

[edited by - Dragon88 on February 6, 2004 4:33:16 PM]

[edited by - Dragon88 on February 6, 2004 4:33:54 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Don''t use itoa.

Share this post


Link to post
Share on other sites
quote:

Don''t use itoa.


yeah, don''t use itoa....or printf, cout, cin, include, define, main() and everything else.

Let him use what he/she wants to use ok. Unless you have a reason....in which you can include with your post.

my 3 cents.

Share this post


Link to post
Share on other sites
quote:

strcat strTemp with atof(aNumber). atof returns a char * version of a floating



No. atof returns a floating point variable.

try this:
char buffer[ 20 ];
fprintf( buffer, "%s%.2f", strTemp, aNumber );

printf( "%s", buffer );

Share this post


Link to post
Share on other sites
So in essence, you are telling him what I said?



(Stolen from Programmer One)
UNIX is an operating system, OS/2 is half an operating system, Windows is a shell, and DOS is a boot partition virus

Share this post


Link to post
Share on other sites

#include<sstream>
#include<string>
#include<iostream>

int main()
{
float f=2.f;
std::ostringstream o;
o << "Hello" << f;
std::string s = o.str();
std::cout << s;
}


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by brain21
quote:

Don''t use itoa.


yeah, don''t use itoa....or printf, cout, cin, include, define, main() and everything else.

Let him use what he/she wants to use ok. Unless you have a reason....in which you can include with your post.



Everyone should know the reason already: itoa isn''t standard.

Everything else you mention is part of the language standard or the standard library... assuming you mean "int main()" and not "void main()".

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Aface
Jingo''s method is safest.


Also it''s the only method posted in this thread so far that actually works.

Share this post


Link to post
Share on other sites
template<class T>
std::string toString(const T& thing) {
std::ostringstream ss;
ss << thing;
return ss.str();
}

std::string f = toString(3.142f);
std::string d = toString(3.1415926535897932);
std::string n = toString(153);
std::string c = toString(std::complex<double>(1.7, 6.5));


edit: changed to ostringstream

[edited by - petewood on February 11, 2004 8:30:14 AM]

Share this post


Link to post
Share on other sites
Pete,

I have found that there is a default number of significant numbers that the stringstream will accept (determined by the type being streamed onto it). Is there a simple way to have a variable number of significant figures (ie that change the number streamed to the number contained in the float?)

Alex

Share this post


Link to post
Share on other sites
Just set the std::ios_base::fixed flag and use the setprecision manipulator to change the number of decimal places, something a bit like this:


#include<sstream>
#include<string>
#include<iostream>
#include<iomanip>
#include<stdexcept>
#include<cstddef>

template<typename T, typename chartype, class traits, class alloc>
std::basic_string<chartype, traits, alloc>& append(const T& num,
std::basic_string<chartype, traits, alloc>& str,
std::size_t precision_param = 0)
{
std::basic_ostringstream<chartype, traits, alloc> o;
o << std::fixed << std::setprecision(precision_param);
if(!(o << num))
throw std::logic_error("invalid conversion");
str+=o.str();
return str;
}


int main()
{
std::string s1, s2;
append<float>(10.213123, s1, 2);
append<float>(10.213123, s2, 10);
std::cout << s1 << std::endl << s2 << std::flush;
}


You can set the precision by changing the 3rd parameter passed to append




[edited by - Jingo on February 12, 2004 5:41:59 AM]

Share this post


Link to post
Share on other sites

  • Advertisement