Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String to float and float to string?

Status
Not open for further replies.

rastkocvetkovic

Programmer
Aug 11, 2002
63
SI
My script looks like simply this:

char string[100], *pointerToString;
pointerToString = string;
string = "124.35";
float myFloat;
myFloat = atof(string);

That doesn't work, I don't know why. I have tried to include all the libraries I could imagine, and still nothing. What could be the problem?

And now, vice versa:

I want to output a float to a file, therefore float should be transformed to string. How could I do that?

I'll be very glad of any help. Thanks!
 
Im not 100% sure about this, but:
I looked up atof in MSN Library and it looks like it can only change strings to doubles:

double atof( const char *string );

There was NOT a
float atof( const char *string);

So I assume that you will need to use a double instead of a float, OR write your own function :)

As far as converting a string to a float / double: I have no idea. I never had heard of atof before last week since I am a new programmer. Hopefully someone else can help you out w/ this one.

 
Do not use string = "124.35". Use strcpy(string, "124.35"). For char[] and char* you have to use strcpy, strncpy, memcpy or memmove. That should solve your problem.

To convert float to char[] use sprintf :
sprintf(string, "%f", 124.34);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top