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
1
vote
2 answers

atof conflict while compiling code

I have just begun my journey with C programming. I have a problem with a lib conflict. Please find my sample code below. #include #include #define MAXLINE 100 //int getline declaration is here (removed for simplicity) int…
1
vote
1 answer

extract from stringstream into 2D vector

This might be a stupid question, but anyway: I would like to obtain the numbers from a .txt file, which contains an adjacency matrice of a graph, the first row of the file contains only the number of nodes. 10 -1 5 3 -1 -1 -1 -1 -1 -1 -1 5 -1 -1 4…
user3530026
1
vote
3 answers

Reading ASCII numbers using "D" instead of "E" for scientific notation using C

I have a list of numbers which looks like this: 1.234D+1 or 1.234D-02. I want to read the file using C. The function atof will merely ignore the D and translate only the mantissa. The function fscanf will not accept the format '%10.6e' because it…
Escualo
  • 40,844
  • 23
  • 87
  • 135
1
vote
2 answers

How do I implement atof (ascii to float) method in ruby?

I am trying to make an RPN calculator. I have to implement my own .to_i and .to_f method. I cannot use send, eval, Float(str) or String(str) method. The assignment is done, but I still want to know how to implement it. The input: atof("255.25") …
1
vote
3 answers

How to convert a values i get from a file (char) and store the values in to an array of double?

I am reading data from a file, retrieving how many columns and rows I have ( data file ), everything of so far. Now I am trying to read the values one by one and store the values in to a 2D array (double). I get the values as char using getc but…
mrmurmanks
  • 33
  • 12
1
vote
2 answers

Standard C function atof returns segmentation faults when using with strtok

I have problem with using atof and strtok. #include // printf #include // atof #include // strtok int main() { char buf[256]="123.0 223.2 2314.2"; char* tp; printf("buf : %s\n", buf); tp = strtok(buf," "); …
10ants
  • 255
  • 3
  • 10
1
vote
2 answers

C++: atof() has wrong behaviour

I am using a library for loading Wavefront .obj files into my OpenGL application (tinyobjloader). I noticed that there is an error when loading objects. When I load an object with a coordinate of eg. 0.9999999 it is set to 0. By debugging I found…
Tim
  • 135
  • 1
  • 12
1
vote
2 answers

C++ converting string to double using atof

I cannot get the atof() function to work. I only want the user to input values (in the form of decimal numbers) until they enter '|' and it to then break out the loop. I want the values to initially be read in as strings and then converted to…
Shaun1810
  • 317
  • 3
  • 5
  • 15
1
vote
4 answers

C++ error converting a string to a double

I am trying to convert a string to a double. The code is very simple. double first, second; first=atof(str_quan.c_str()); second=atof(extra[i+1].c_str()); cout<
piratebill
  • 31
  • 1
  • 3
1
vote
6 answers

C convert section of char array to double

I want to convert a section of a char array to a double. For example I have: char in_string[] = "4014.84954"; Say I want to convert the first 40 to a double with value 40.0. My code so far: #include #include int main(int arg)…
Kyle Weller
  • 2,533
  • 9
  • 35
  • 45
1
vote
2 answers

Does any one know of a atolf c function?

I am looking for a function similar to atol (char * to long int) but atofl (char to long double), does anyone know of a library that does this, or a simple way to do this, since using atof on "0.00000005" casts off the 5? Thanks.
mihajlv
  • 2,275
  • 4
  • 36
  • 59
0
votes
5 answers

Function atof() rounds its result to integer part

Why does atof round "14718.5084" to 14718.5? Is there a way to prevent that (i.e. get the whole number 14718.5084)? code: double latitude=atof("14718.5084"); std::cout <<"latitude test "<
user1106106
  • 277
  • 2
  • 7
  • 13
0
votes
2 answers

set precision when push_back on Vector

I'm reading from a CSV, line by line and tokenizing each comma separated value. each token is a string type. and I'm putting it into a vector of type float. In the example below, if for example if the value in the csv is "0.08" , *beg = "0.08" , but…
Lexicon
  • 2,467
  • 7
  • 33
  • 41
0
votes
0 answers

Variable changes unintentionally after character Lcd is moved to display other variables

There is a project of mine which utilizes coordinates to be entered by the user and then processing them for sunrise and sunset time calculations. Although everything seems to work fine but there is something which I noticed recently and that is…
0
votes
2 answers

What does atof stand for?

In C atof=a-to-f(loat) converts a string into a double precision float. I am wondering what the a part of atof stand for.
zmkm
  • 117
  • 1
  • 4
1 2 3
8 9