Questions tagged [atof]

atof() is the C runtime library function for converting the ASCII representation of a number to a floating point double.

Use this tag on for all questions about the use of atof() or where it does not seem to be working correctly.

Closely related are:

  • for functions converting text to int, long, or long long
  • for converting to long from text in any base from 2 through 36. Or choose a base automatically as the C compiler does depending on how the number is written. strtoul() converts to an unsigned long.
  • for double
  • for converting one or more values at a time directed by a format specification

SYNPOSIS

#include <stdlib.h>

double atof(const char *nptr);

atof() returns a value of zero if there is any problem during the conversion, though the documentation says that it does not detect errors. Since converting a zero also returns zero, there is no easy way of distinguishing an error from a correct conversion of zero. Use either scanf() or strtod() if error checking is needed.

atof() accepts integers, fixed decimal notation, and scientific notation. Some implementations also recognize the strings INF or INFINITY (disregarding case), NAN (disregarding case) optionally followed by (, a sequence of characters, followed by ). The character string specifies an implementation-dependent type of NAN. (Documentation of these strings are scant.)

Some implementations support hexadecimal formatted floats. These begin with 0x or 0X, followed by at least one hex digit, an optional decimal point (locale-specific), more digits, an optional p or P followed by the binary exponent in hex.

129 questions
62
votes
9 answers

How to convert string to float?

#include #include int main() { char s[100] ="4.0800" ; printf("float value : %4.8f\n" ,(float) atoll(s)); return 0; } I expect the output should be 4.08000000 whereas I got only 4.00000000. Is there any way to…
abubacker
  • 4,638
  • 6
  • 32
  • 35
27
votes
6 answers

atoi implementation in C

I can't understand the following atoi implementation code, specifically this line: k = (k << 3) + (k << 1) + (*p) - '0'; Here is the code: int my_atoi(char *p) { int k = 0; while (*p) { k = (k << 3) + (k << 1) + (*p) - '0'; …
Adam
  • 1,944
  • 3
  • 17
  • 19
26
votes
7 answers

Locale-independent "atof"?

I'm parsing GPS status entries in fixed NMEA sentences, where fraction part of geographical minutes comes always after period. However, on systems where locale defines comma as decimal separator, atof function ignores period and whole fraction…
tomash
  • 12,742
  • 15
  • 64
  • 81
21
votes
8 answers

converting string to a double variable in C

I have written the following code. It should convert a string like "88" to double value 88 and print it void convertType(char* value) { int i = 0; char ch; double ret = 0; while((ch = value[i])!= '\0') { ret = ret*10 + (ch -…
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90
15
votes
3 answers

Converting char* to float or double

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…
Andrew
  • 830
  • 3
  • 10
  • 27
8
votes
2 answers

atof() for float instead of double

atof() returns a double, which results in a warning when I assign it to a float-value (and yes, I definitively have to use float). So my question: is there a atof()-variant available which returns a plain float? Or do I have to solve this by a cast…
Elmi
  • 5,899
  • 15
  • 72
  • 143
7
votes
2 answers

const char* to double translation issue with C++

I have two sample applications using the same library and the main difference between them is that one uses qt and the other application is a console application. In the common library, I have this test code: double test = 0.1; double test2 =…
goe
  • 2,272
  • 1
  • 19
  • 34
6
votes
2 answers

Android FFmpeg : Undefined references to atof, log2 & log2f

I am trying to link to FFmpeg built for android using android-ndk-r15c. I built this by downloading FFmpeg source that is latest ffmpeg-3.3.4. Following are my linker list: -lavformat -lavcodec -lswscale -lavutil -lavfilter -lswresample -lavdevice…
TheWaterProgrammer
  • 7,055
  • 12
  • 70
  • 159
6
votes
0 answers

std::atof or std::stof (c++11) unable to convert properly ("1.24")

I'm a little bit frustrated after I've found this strange atof/stof behavior double bg = std::stof("1,24"); std::cerr<<"comma: "<
bartux
  • 111
  • 5
5
votes
4 answers

unable to convert contents in argv[] into float[][] in C

I am doing a program where I'm multiplying matricies, but my big issue is converting from the input into the two arrays that I'll eventually be multiplying. The following is my code for conversion including the declaration of the arrays. (I removed…
Jonathan
  • 479
  • 3
  • 8
  • 17
5
votes
1 answer

atof and stringstream produce different results

I have been looking into a problem whereby I am converting a float to a human readable format, and back. Namely a string. I have ran into issues using stringstream and found that atof produces "better" results. Notice, I do not print out the data in…
Asheh
  • 1,547
  • 16
  • 25
5
votes
2 answers

What is wrong with usage of atof function?

int main() { char str[10]="3.5"; printf("%lf",atof(str)); return 0; } This is a simple code I am testing at ideone.com. I am getting the output as -0.371627
Naved Alam
  • 827
  • 9
  • 25
4
votes
5 answers

Not including stdlib.h does not produce any compiler error!

Hopefully this is a very simple question. Following is the C pgm (test.c) I have. #include //#include int main (int argc, char *argv[]) { int intValue = atoi("1"); double doubleValue = atof("2"); …
Bala
  • 3,938
  • 3
  • 19
  • 17
4
votes
2 answers

atof() is returning ambiguous value

I am trying to convert a character array into double in c using atof and receiving ambiguous output. printf("%lf\n",atof("5")); prints 262144.000000 I am stunned. Can somebody explain me where am I going wrong?
alDiablo
  • 969
  • 7
  • 22
4
votes
1 answer

boost lexical cast string to double

I am facing a conversion issue for which I'd like your help. I'm using gcc4 compiler and I am quite restricted to use gcc4. I want to convert std::string to double. std::string aQuantity = aRate.getQuantity(); std::string aAmount =…
user1400995
1
2 3
8 9