Convert char * to float or double

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

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

Other Edit:

I do exactly that 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 result is the 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 a very simple name, pairs of values, such as:

 NAME|VALUE NAME|VALUE NAME|VALUE 

with a value of dollar amounts

SOLVED: Thanks to everyone, I used% d instead of% f and did not have the correct headers.

+7
source share
3 answers

You are missing the include: #include <stdlib.h> , so GCC creates an implicit atof and atod , which results in garbage values.

And the format specifier for double is %f , not %d (i.e. 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 
+21
source

The code you submitted is correct and should work. But check what you have in char* . If the correct value is large to be represented, the functions will return a positive or negative HUGE_VAL . Check what char* you have for the maximum values ​​that float and double can display on your computer.

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

I tried the example that you provided on both Windows and Linux, and it worked fine.

+1
source
 printf("price: %d, %f",temp,ftemp); ^^^ 

It's your problem. Since the arguments are of type double and float , you should use %f for both (since printf is a variational function, ftemp will be upgraded to double ).

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

Variadic functions, such as printf , do not know the argument types in the variable argument list; you have to say this with the conversion specifier. Since you told printf that the first argument must be int , printf will take the next sizeof (int) bytes from the argument list and interpret it as an integer value; hence the first trash number.

It is now almost guaranteed that sizeof (int) < sizeof (double) , so when printf takes the next sizeof (double) bytes from the argument list, it probably starts with the middle byte temp , not the first byte ftemp ; hence the second trash number.

Use %f for both.

0
source

All Articles