Converting char* to float or double
Asked Answered
H

3

15

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.

Hobbema answered 15/5, 2012 at 17:11 Comment(4)
The problem must be somewhere else, the code you posted is perfectly valid and working. Please show us how you read your file.Silvia
1. atof returns double as well: pubs.opengroup.org/onlinepubs/007904875/functions/atof.htmlResistless
2. %d is for integers. Use %f.Resistless
3. You forgot #include <stdlib.h>Resistless
S
31

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
Silvia answered 15/5, 2012 at 17:40 Comment(1)
+1. Right. This is the problem. [before OPs edits, there were actually three problems]Resistless
H
1

Code posted by you is correct and should have worked. But check exactly what you have in the char*. If the correct value is to big to be represented, functions will return a positive or negative HUGE_VAL. Check what you have in the char* against maximum values that float and double can represent on your computer.

Check this page for strtod reference and this page for atof reference.

I have tried the example you provided in both Windows and Linux and it worked fine.

Hindquarter answered 15/5, 2012 at 17:26 Comment(3)
According to: pubs.opengroup.org/onlinepubs/007904875/functions/atof.html (which is aligned with the C standard): "If the value cannot be represented, the behavior is undefined."Resistless
The values are all much smaller than the max either variable type can represent, the largest value I have is 120.55.Hobbema
@George: don't use cplusplus.com. It's full of errors. Neither atof nor strtod return HUGE_VAL in case of an overflow. According to cppreference, the return value is undefined.Ozone
T
0
printf("price: %d, %f",temp,ftemp); 
              ^^^

This is your problem. Since the arguments are type double and float, you should be using %f for both (since printf is a variadic function, ftemp will be promoted to double).

%d expects the corresponding argument to be type int, not double.

Variadic functions like printf don't really know the types of the arguments in the variable argument list; you have to tell it with the conversion specifier. Since you told printf that the first argument is supposed to be an int, printf will take the next sizeof (int) bytes from the argument list and interpret it as an integer value; hence the first garbage number.

Now, it's almost guaranteed that sizeof (int) < sizeof (double), so when printf takes the next sizeof (double) bytes from the argument list, it's probably starting with the middle byte of temp, rather than the first byte of ftemp; hence the second garbage number.

Use %f for both.

Tuneberg answered 15/5, 2012 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.