Converting char* to float or double

Andrew picture Andrew · May 15, 2012 · Viewed 126.6k times · Source

I have a value I read in from a file and is stored as a char*. The value is a monetary number, #.##, ##.##, or ###.##. I want to convert the char* to a number I can use in calculations, I've tried atof and strtod and they just give me garbage numbers. What is the correct way to do this, and why is the way I am doing it wrong?

This is essentially what I am doing, just the char* value is read in from a file. When I print out the temp and ftemp variables they are just garbage, gigantic negative numbers.

Another Edit:

I am running exactly this in gcc

#include <stdio.h>
int main()
{
 char *test = "12.11";
 double temp = strtod(test,NULL);
 float ftemp = atof(test);
 printf("price: %f, %f",temp,ftemp);
 return 0;

}

and my output is price: 3344336.000000, 3344336.000000

Edit: Here is my code

if(file != NULL)
    {
        char curLine [128];
        while(fgets(curLine, sizeof curLine, file) != NULL)
        {               
            tempVal = strtok(curLine,"|");          
            pairs[i].name= strdup(tempVal);
            tempVal = strtok(NULL,"|");
            pairs[i].value= strdup(tempVal);
            ++i;
        }
        fclose(file);
    }

    double temp = strtod(pairs[0].value,NULL);
    float ftemp = atof(pairs[0].value);
    printf("price: %d, %f",temp,ftemp);

my input file is very simple name, value pairs like this:

NAME|VALUE
NAME|VALUE
NAME|VALUE

with the value being dollar amounts

SOLVED: Thank you all, I was using %d instead of %f and didn't have the right headers included.

Answer

user703016 picture user703016 · May 15, 2012

You are missing an include : #include <stdlib.h>, so GCC creates an implicit declaration of atof and atod, leading to garbage values.

And the format specifier for double is %f, not %d (that is for integers).

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

int main()
{
  char *test = "12.11";
  double temp = strtod(test,NULL);
  float ftemp = atof(test);
  printf("price: %f, %f",temp,ftemp);
  return 0;
}
/* Output */
price: 12.110000, 12.110000