code:

Can't Copy and Paste this?

Click here for a copy-and-paste friendly version of this code!

 

Terms of Agreement:   

By using this code, you agree to the following terms...   

1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for langauges that allow it) freely and with no charge.   

2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   

3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 

4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.  

/*

atod function by Alex Lowe

April 25, 1998 3:32AM v1.2

alowe@iadvance.net

www.iadvance.net

www.templecrc.com

Description:

Takes a string and converts it to double floating point.

Support for negative signs and decimal points in the string.

Syntax:

double atod(char *string)

Required Functions:

math.h [pow()], string.h [strlen()]

*/

#include 

#include 

#include 

double atod(char *buf)

    {

    double mult=0, total=0;

    int i,j;

    int decloc = 0;

    for (j=0, i=strlen(buf)-1; i >= 0; --i, ++j)

         {

        if (buf[i] == '.')

            {

            decloc = j;

            i--;

        }

        

         if (buf[i]<='9' && buf[i]>='0')

         total += (buf[i]-'0') * pow(10,j);

    }

    if (buf[0] == '-')

    total = -total;

    return total/pow(10,decloc);

}

void main()

    {

    double num;

    num=atod("-123.456789");

    printf ("\n%f\n", num);

}