Is there a function similar to atoi which converts a string to float instead of to integer?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
-
What is the difference between
and in java - How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
Prefer
strtof()
.atof()
does not detect errors.Use
atof
fromstdlib.h
:This would also work ( but C kind of code ):
See it here: http://codepad.org/qlHe5b2k
As an alternative to the the already-mentioned
std::strtof()
andboost::lexical_cast<float>()
, the new C++ standard introducedfor error-checking string to floating-point conversions. Both GCC and MSVC support them (remember to
#include <string>
)atof()
(or
std::atof()
talking C++ - thanks jons34yp)strtof
From the man page
The strtod(), strtof(), and strtold() functions convert the initial portion of the string pointed to by nptr to double, float, and long double representation, respectively.
The expected form of the (initial portion of the) string is optional leading white space as recognized by isspace(3), an optional plus (‘‘+’’) or minus sign (‘‘-’’) and then either (i) a decimal number, or (ii) a hexadecimal number, or (iii) an infinity, or (iv) a NAN (not-a-number).
/man page>
atof converts a string to a double (not a float as it's name would suggest.)