Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C - <b>atof() convert string to double</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
atof()                   Convert String to Double

 #include   <math.h>
 #include   <stdlib.h>                   Use either math.h or stdlib.h

 double     atof(string);                Value of converted string
 char       *string;                     String to be converted


    atof() converts 'string' to a double-precision floating-point value.
    'string' is a sequence of characters that can be interpreted as a
    numerical value; it has the following format:

     [whitespace][sign][digits][.digits][{d|D|e|E}[sign]digits]

    where:

        whitespace  any number of tab and space characters are ignored
        sign        + or -
        digits      one or more decimal digits
        d|D|e|E     exponent prefixes

    If no digits appear before the decimal point, then at least one must
    appear after the decimal point. (If a decimal point is present, it
    must be followed by a digit.)  atof() stops reading input at the
    first character it cannot recognize as part of the number (likely the
    null character terminating the string).

    Returns:    The double value of 'string'.  If 'string' cannot be
                converted to a double value, then 0.0 is returned.  The
                return value is undefined for overflow; 0.0 is returned
                on underflow.

      Notes:    DBL_MAX_EXP (defined in <limits.h>) is the maximum
                allowed exponent for double values.

  -------------------------------- Example ---------------------------------

    The following statements convert several strings to double values and
    print the values.

         #include <math.h>
         #include <stdio.h>

         main()
         {
             double x, y, z;

             x = atof("    -.625d-3  (this will be -0.000625)");
             y = atof("0");
             z = atof("  143248291283472348923874.2134762734E222\n");

             /* prints: x=-6.250000e-004, y=0.000000e+000, z=1.432483e+245 */
             printf("x=%e, y=%e, z=%e\n", x, y, z);
         }

See Also: atoi() atol() ecvt() fcvt() gcvt()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson