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

 #include   <stdlib.h>

 double     strtod(sptr,endptr);
 const char *sptr;                       Pointer to string
 char       **endptr;                    Pointer to end of scan

    strtod() converts a string 'sptr' to a double value.  The string
    'sptr' is a sequence of characters that can be interpreted as a
    double value. The string must match this sequence:

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

    strtod() stops reading the string 'sptr' at the first character that
    cannot be recognized as part of a double value.  'endptr' points to
    the character that stopped the scan, which may be the null character
    that terminated the string.

       Returns:     The value of the floating-point number. In case of
                    overflow, it returns 'HUGE_VAL'.

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

    The following statements convert two strings into doubles, add them
    together, and print the total.

           #include <stdlib.h>
           #include <stdio.h>

           double total;
           double dubl1, dubl2;
           char *str1 = "982.50 dollars", *str2 = "540.00 dollars";
           char *txt1, *txt2;

           main()
           {
           /* The space before "dollars" will stop the scanning of string. */
               dubl1 = strtod(str1,&txt1);
               dubl2 = strtod(str2,&txt2);
               total = dubl1 + dubl2;
               printf("TOTAL: %.2f ",total);
            }


See Also: strtol() atof() atol()

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