Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting char* to float or double

Tags:

c

atof

strtod

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.

like image 497
Andrew Avatar asked May 15 '12 17:05

Andrew


People also ask

Can char convert to float?

The atof() function converts a character string to a double-precision floating-point value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.

Can char convert to double?

We can convert a char array to double by using the std::atof() function.

Can you convert char to float in C?

Description. The C library function double atof(const char *str) converts the string argument str to a floating-point number (type double).

Can we convert char to float in C++?

unsigned char c='a'; float f=(float)(c);//by explicit casting float fc=c;//compiler implicitly convert char into float. unsigned char c='9'; float f=(float)(c-'0');//by explicit casting float fc=c-'0';//compiler implicitly convert char into float.


1 Answers

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
like image 179
user703016 Avatar answered Oct 27 '22 08:10

user703016