atof()
returns a double, which results in a warning when I assign it to a float-value (and yes, I definitively have to use float).
So my question: is there a atof()
-variant available which returns a plain float? Or do I have to solve this by a cast - which would be a pity because it wastes resources for creating a double which will be thrown away immediately.
The atof() function converts a character string to a double-precision floating-point value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.
float and double both have varying capacities when it comes to the number of decimal digits they can hold. float can hold up to 7 decimal digits accurately while double can hold up to 15.
Syntax of atof() functiondouble atof (const char* str); It accepts pointer constant to character str which is string representation of floating pointer number. On success, it returns converted floating point number as double type. If the given string str has no possible conversion then returns 0.0 .
Defined in header <stdlib.h> double atof( const char* str ); Interprets a floating-point value in a byte string pointed to by str . Function discards any whitespace characters (as determined by std::isspace()) until first non-whitespace character is found.
Use strtof
instead, it will return a float.
See documentation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With