Is there a function similar to atoi which converts a string to float instead of to integer?
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.
So you can replace atoi() with strtol(nptr, NULL, 10) everywhere. As for improving error checking, see the documentation of strtol and what it returns upon different errors.
atof recognizes an optional string of tabs and spaces, then an optional sign, then a string of digits optionally containg a decimal point, then an optional e or E followed by an optionally signed integer. atoi recognizes an optional string of tabs and spaces, then an optional sign, then a string of digits.
First, atoi() converts C strings (null-terminated character arrays) to an integer, while stoi() converts the C++ string to an integer. Second, the atoi() function will silently fail if the string is not convertible to an int , while the stoi() function will simply throw an exception.
atof()
(or std::atof()
talking C++ - thanks jons34yp)
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