convert a string to a double
#include <stdlib.h> double strtod( const char *ptr, char **endptr );
The strtod() function converts the string pointed to by ptr to double representation. The function recognizes a string containing the following:
The conversion ends at the first unrecognized character. A pointer to that character will be stored in the object to which endptr points if endptr is not NULL. You can determine how much of the string (if any) was scanned by comparing the "end" pointer with endptr.
The strtod() function returns the converted value. If the correct value would cause overflow, plus or minus HUGE_VAL is returned according to the sign, and errno is set to ERANGE. If the correct value would cause underflow, then zero is returned, and errno is set to ERANGE.
Zero is returned when the input string cannot be converted. When an error has occurred, errno contains a value indicating the type of error that has been detected.
#include <stdio.h> #include <stdlib.h> void main() { double pi; pi = strtod( "3.141592653589793", NULL ); printf( "pi=%17.15f\n",pi ); }
ANSI
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |