C examples for stdlib.h:strtod
function
<cstdlib> <stdlib.h>
Convert string to double
double strtod (const char* str, char** endptr);
Parameter | Description |
---|---|
str | C-string representation of a floating-point number. |
endptr | an char* to the next character in str after the numerical value. This parameter can be a null pointer if it is not used. |
On success, the function returns the converted floating point number as a value of type double.
For non valid conversion, the function returns 0.0.
For out of the range values, a positive or negative HUGE_VAL is returned, and errno is set to ERANGE.
#include <stdio.h> #include <stdlib.h> int main ()/*from ww w .j a va2 s . com*/ { char str[] = "1234.1234 123.53 test"; char* pEnd; double d1, d2; d1 = strtod (str, &pEnd); d2 = strtod (pEnd, NULL); printf ("%f\n", d1); printf ("%f\n", d2); return 0; }