P
pontiy.pilat
How i can convert *char to float?
How i can convert *char to float?
Chris said:atof();
Chris said:atof();
Eric said:strtod() is better, but neither actually converts a
string to float: both convert to double. The only way I
can think of to convert directly to float is
if (sscanf(my_string, "%f", &float_variable) == 1)
Robert Gamble said:C99 has strtof() which will convert directly to float representation.
Keith said:And if your implementation doesn't have strtof(), it's easy enough to
call strtod() and assign the result to a float.
It's even easier to use double in the first place.
Agreed.
sscanf() has the disadvantage that, if the floating-point literal in
the string represents a value that can't be represented in the target
type, the behavior is undefined. strtod() has well-defined behavior
on overflow. (It would have been nice if sscanf() had been defined
this way as well.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.