strtod -- Convert Character String to Double

Format

#include <stdlib.h>
double strtod(const char *nptr, char **endptr);

Language Level: ANSI, XPG4
strtod converts a character string to a double-precision value. The parameter nptr points to a sequence of characters that can be interpreted as a numerical value of the type double. When it reads a character that it does not recognize as part of a number, strtod stops reading the string at that character, and sets endptr to point to the remainder of nptr. The character at which strtod stops reading the string may be the null character at the end of that string.

On both OS/2 and Windows, the strtod function expects nptr to point to a string with the following form:

Note: The character used for the decimal point (shown as . in the above diagram) depends on the LC_NUMERIC category of the current locale.

The first character that does not fit this form stops the scan.

Return Value
strtod returns the value of the floating-point number, except when the representation causes an underflow or overflow. For an overflow, it returns -HUGE_VAL or +HUGE_VAL; for an underflow, it returns 0.

In both cases, errno is set to ERANGE, depending on the base of the value. If the string pointed to by nptr does not have the expected form, no conversion is performed and the value of nptr is stored in the object pointed to by endptr, provided that endptr is not a NULL pointer.

strtod does not fail if a character other than a digit follows an E or e read in as an exponent. For example, 100elf will be converted to the floating-point value 100.0.

Example
This example converts the strings to a double value. It prints out the converted value and the substring that stopped the conversion.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
   char *string, *stopstring;
   double x;
   string = "3.1415926This stopped it";
   x = strtod(string, &stopstring);
   printf("string = %s\n", string);
   printf("   strtod = %lf\n", x);
   printf("   Stopped scan at %s\n\n", stopstring);
   string = "100ergs";
   x = strtod(string, &stopstring);
   printf("string = \"%s\"\n", string);
   printf("   strtod = %lf\n", x);
   printf("   Stopped scan at \"%s\"\n\n", stopstring);
   return 0;
   /***************************************************
      The output should be:
      string = 3.1415926This stopped it
         strtod = 3.141593
         Stopped scan at This stopped it
      string = "100ergs"
         strtod = 100.000000
         Stopped scan at "ergs"
   ***************************************************/
}


atof -- Convert Character String to Float
atoi -- Convert Character String to Integer
atol -- Convert Character String to Long Integer
_atold -- Convert Character String to Long Double
strtol -- Convert Character String to Long Integer
strtold -- Convert String to Long Double
strtoul -- Convert String Segment to Unsigned Integer
wcstod -- Convert Wide-Character String to Double
wcstol -- Convert Wide-Character to Long Integer
wcstoul -- Convert Wide-Character String to Unsigned Long
<stdlib.h>