-3

Possible Duplicate:
Converting a string into a double

I need to convert " 5.71" to 5.71 in C.

I am reading in a string as a token, but need to store the value as a double.

Are there any functions that do this? And what print statement should I be using?

char *number = "5.71";
printf("%???", double);

Any help would be great!

Community
  • 1
  • 1
user1880514
  • 87
  • 1
  • 1
  • 3
  • 1
    [converting a string into a double](http://stackoverflow.com/questions/4308536/converting-a-string-into-a-double), [Converting a string to double](http://stackoverflow.com/questions/5601537/converting-a-string-to-double) – ljedrz Dec 11 '12 at 19:48

3 Answers3

1

You are looking for strtod().

And what print statement should I be using?

The format specifier for double is %lf.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Consider using

sscanf(number, "%f", &doubleVar);

That should be the 'standard' way to do it.

ATaylor
  • 2,598
  • 2
  • 17
  • 25
0

Are there any functions that do this?

Yes, strtod, strtof and strtold convert strings to double, float and long double, respectively. For example:

float f = strtof("2.718281829", NULL);

And what print statement should I be using?

Euh, they're rather called format specifiers. Also they vary, depending on the exact type:

float f = 3.14;
double d = 3.14;
long double ld = 3.14;

printf("%f %lf %llf\n", f, d, ld);