Click here to Skip to main content
15,884,388 members

String to float conversion

Revision 1
Hello,

Could you please let me know how to convert string to float without using standard functions, like atoi, atof, strtod, sscanf etc.
My code is:
main()
{
char inputstr[]="123.45";
float result= 0.0F;
float temp;
int ndecimaldigit,i;
ndecimaldigit = 1;
for(i=0;i<=4;i++)
{
temp=inputstr[i] - '0';
ndecimaldigit *= 10;
temp /= ndecimaldigit;
result += temp;
}
printf("%f %f %d \n",result,temp,ndecimaldigit);
}

But i am getting output as: 0.122671 -0.000129 -31072
which is not correct. I need output as 123.45
Posted 3-May-12 0:24am by Member 8647682.
Tags: ,