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
-2
votes
1 answer

Why does the atof function not work for me? C++

I try to write some vectors in a file but the atof function gives me problems and I don't know how to solve it. Up to the fscanf line (read, "% s% s \ n", s1, s2); it doesn't present problems to me, then yes. #include #include
lm01010
  • 1
  • 1
-2
votes
2 answers

atof("0") returns 2 in float variable

I write c embedded on STM32F437VI. At some point, I need to parse some strings that contain numbers, as float numbers. I use atof and it works always with the correct result, except for this one weird case. If the string is 0 or 0.0 it gives 2. I…
BabisI
  • 25
  • 1
  • 7
-2
votes
2 answers

Wrong behaviour of atof()

Sample.c: #include #include main() { char str[10] = "111.1"; float f = (float)atof(str); printf ("\n (%s , %f) \n",str,f); } Output of above code is : (111.1 , 111.099998) Please suggest whether i am missing…
AkhilT
  • 21
-2
votes
3 answers

Converting string with decimal to double using atof in C++

In my program im trying to read in a file with money like this "$876,725.38" I was able to remove the comma and dollar sign and I am now trying to convert the string to a double using atof but the issue im having now is after printing MONEYS after…
-2
votes
1 answer

atof using atoi Error

This is a atof function that converts string to float by first converting the string in to integer then dividing the integer number by 10s to get the real number after saving the deciaml point position Although the condition is true, the program…
Mostafa Ezz
  • 55
  • 1
  • 6
-3
votes
1 answer

Float print gives extra numbers

i am having a hard time printing float variable. It gives me extra numbers, that shouldn´t be there. Here is example: float number; char temp[50]; fgets ( temp, sizeof temp, fr ); //reading string from file, example: 99.10 …
-4
votes
3 answers

convert string to double , the answer is correct ,but not very accurate

I want to use atof to convert my string to a double,the answer is correct ,but not very accurate ATTENTION: because of some other reasons, fscanf is not permitted my code is : #include #include #define MAXCN 50 int main(void) {…
KatrinaKing
  • 43
  • 2
  • 6
-6
votes
1 answer

Do I need to include and for c_str(), atoi and atof functions?

I'm using c_str(), atoi and atof functions for converting string variables to integer or float/double. For example, val = atoi(val1.c_str()); val = atof(val1.c_str()); So, I would like to know if I need to include and . Thanks.
-8
votes
4 answers

What is wrong with this code? I need to convert string into float

What is wrong with this code? I need to convert string into float. m2 has an error in m2.length #include #include #include #include using namespace std; int main() { float v2; char *m2 = "1 23 45 6"; …
ezzeddin
  • 499
  • 2
  • 5
  • 25
1 2 3
8
9