# function to convert char array to double.

Page 1 of 1

## 5 Replies - 10967 Views - Last Post: 27 October 2011 - 07:50 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=253077&amp;s=f33199b748cfa77675e1040c9a68ea4d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jink

Reputation: 5
• Posts: 63
• Joined: 10-July 11

# function to convert char array to double.

Posted 26 October 2011 - 10:56 PM

I have written a funtion to convert a number like 233.44e-2(in char array) to double.the code for the function is
```#include <stdio.h>
#include<ctype.h>
double atof(char s[])
{
int i,j;//j is for multiplying all ten of decimals
short signed int sign=+1;
short int point=0,pow=0,exp=0;//exp is 0 if exponent is positive and 1 if the exponent is negative
int power=1;
double number=0.0;
while(s[i]=='\t'||s[i]==' '||s[i]=='-'||s[i]=='+')//skipping all unwanted part
{
if(s[i]=='-')
sign=-1;
i++;
}
for(;isdigit(s[i])||s[i]=='.';i++)
{
if(point=1)
pow++;//counting the number of digits after decimal
if(s[i]=='.')
{
point='1';
i++;
}
number=number*10.0+(s[i]-'0');//calculating the entire number ignoring the decimal
}
number*=sign;
for(j=1;j<=pow;j++)
number=number/10.0;//putting proper decimal point for the number
if(s[i]=='e'||s[i]=='E')
{
i++;
if(s[i]=='-')
{
exp=1;
i++;
}
else if(s[i]=='+')
{
exp=0;
i++;
}
for(;isdigit(s[i]);i++)//for collecting the power which is in exponential form
power=power*10+(s[i]-'0');
if(exp=0)
{
for(j=1;j<=power;j++)
number=number*10.0;/*multiplyinging the number with the additional power of ten present in form of exponext*/
}
else if(exp=1)
{
for(j=1;j<=power;j++)
number=number/10.0;//dividing the number with the additional power of ten present in form of exponext
}
}
return number;
}
```
.to test the function I have written another program
```#define N 25
#include<stdio.h>
#include "newkr4-2.c"//file name of above code
double atof(char s[]);
int main()
{
char str[N]="333256E-2";
double y;
y=atof(str);
printf("%f\n",y);
getchar();
return 0;
}
```
.but when I run the test program I always get the value 0.00000.i have tried searching for the error but couldnt get any.i am presently doing the code in dev cpp compiler.any other improvements needed in the code are also welcome.
Also in general I spent quite a lot of time(1/2 hour to 45 mins) in error detection.is it usual?what could I do to avoid it?that is run the programs correctly at the first time?
Jink

Is This A Good Question/Topic? 0

## Replies To: function to convert char array to double.

### #2 imnoob

Reputation: 0
• Posts: 4
• Joined: 26-October 11

## Re: function to convert char array to double.

Posted 26 October 2011 - 11:04 PM

double toString(char *myStr,int p)
{
double myDouble = atof(myStr);
myDouble = myDouble/pow10(p);
return(myDouble);
}

### #3 sk1v3r

Reputation: 231
• Posts: 668
• Joined: 06-December 10

## Re: function to convert char array to double.

Posted 26 October 2011 - 11:55 PM

What is the value of i when the program starts?
Also,
```if(point=1)

```

maybe you want to use '==' here instead of '='.
That should solve some of the problem, if it stil doesn't work post back

### #4 jink

Reputation: 5
• Posts: 63
• Joined: 10-July 11

## Re: function to convert char array to double.

Posted 27 October 2011 - 12:24 AM

yeah i should have initialised i and used the == operator.mistake.but still its giving value 0.00000.you can try running both the codes yourself yourself if you want to verify.modified code
```#include <stdio.h>
#include<ctype.h>
double atof(char s[])
{
int i=0,j;//j is for multiplying all ten of decimals
short signed int sign=+1;
short int point=0,pow=0,exp=0;/*exp is 0 if exponent is positive and 1 if the exponent is negative*/
int power=1;
double number=0.0;
while(s[i]=='\t'||s[i]==' '||s[i]=='-'||s[i]=='+')
{
if(s[i]=='-')
sign=-1;
i++;
}
for(;isdigit(s[i])||s[i]=='.';i++)
{
if(point==1)
pow++;
if(s[i]=='.')
{
point='1';
i++;
}
number=number*10.0+(s[i]-'0');
}
if(s[i]=='e'||s[i]=='E')
{
i++;
if(s[i]=='-')
{
exp=1;
i++;
}
else if(s[i]=='+')
{
exp=0;
i++;
}
for(;isdigit(s[i]);i++)
power=power*10+(s[i]-'0');
for(j=1;j<=pow;j++)
number=number/10.0;
if(exp=0)
{
for(j=1;j<=power;j++)
number=number*10.0;
}
else if(exp=1)
{
for(j=1;j<=power;j++)
number=number/10.0;
}
}
return number;
}

```
.and for testing
```#define N 25
#include<stdio.h>
#include "newkr4-2.c"

double atof(char s[]);
int main()
{
char str[N]="333256E-2";
double y;
y=atof(str);
printf("%6.4f\n",y);
getchar();
return 0;
}

```

### #5 sk1v3r

Reputation: 231
• Posts: 668
• Joined: 06-December 10

## Re: function to convert char array to double.

Posted 27 October 2011 - 02:06 AM

Sorry about the long wait, I wasn't here for a while.
Some more broken ifs
```if(exp=0)
else if(exp=1)

```

Give me a mo while I skim the rest of the code.
And power should start off at 0, not 1. (Think about power = power*10 + (s[i] - '0');

This post has been edited by sk1v3r: 27 October 2011 - 02:08 AM

### #6 jink

Reputation: 5
• Posts: 63
• Joined: 10-July 11

## Re: function to convert char array to double.

Posted 27 October 2011 - 07:50 AM

I finally got the code working right after a few more corrections.Thank you for your help.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }