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
0
votes
2 answers

Strange output when using atof(optarg)

Edit:: Resolved- This was due to a misunderstanding of the use of the getOpt function. I referenced the materials in the man here, on stack overflow (http://linux.die.net/man/3/getopt) and the getOpt documentation on GNU's website here:…
user4049003
0
votes
0 answers

Printf of a value converted with atof

I'm trying to build a project for my SAM3S microcontroller. I am using the atof function to convert a string of characters passed to the micro through UART into a float, but when i want to see the result of the conversion (through printf) the value…
andrecap
  • 9
  • 2
0
votes
1 answer

atof() returns 3 digit number into 2 digits

I am trying to parse a string sent to my arduino uno using serial communication into a float. I want to use these floats to set an RGB LED's color. My issue is, it will only read the first 2 digits. Sorta. Say I entered 100. It will only come out to…
josephoneill
  • 853
  • 2
  • 10
  • 33
0
votes
1 answer

Converting string to float with atof produces very small decimal numbers at the end

I have a string that stores a decimal value, for instance, "0.10". I want to convert this to a float. But when I use atof to do it, the number I get is not exactly the value it should be. I'm writing some complicated algorithms involving some number…
A. Duff
  • 4,097
  • 7
  • 38
  • 69
0
votes
1 answer

convert float in char array with atof C++

I've been looking for similar problems but am still unable to find a solution to my problem. I am taking a text file and converting it to a binary file to be used in another program. The other program compiles and outputs everything just fine but…
Mary Martinez
  • 314
  • 5
  • 12
0
votes
1 answer

atof() assining 0 where needs to be a dobule

I'm trying to write my school assignment of rpn clalculator with providing expression by command line. Ex ./calc 2 + 5 \* sin 78 My idea is to use the struct data type to keep the number value or the operator type. Here is the main to clarify…
advena
  • 83
  • 13
0
votes
2 answers

Converting string to a double and back to a string

I'm having issues converting a string to a double and not exactly sure what's wrong. My add function: int add(const char *a,const char *b,char* str,int length) { printf("\n*you are in add function %s,%s*\n",a,b); //double aa = strtod(a,NULL); …
dspaces1
  • 193
  • 1
  • 3
  • 15
0
votes
2 answers

atof randomly not working correclty

I seem to be having some weird problem with atof() function not converting some values correctly. For the same values atoi() works perfectly. Here's a little snippet of what I'm doing: ... // frequencies is a std::string in format a10d50f20 etc.…
0
votes
1 answer

Android ndk atof()

Hi i have an android project which uses ndk there is a c function atof() which is defined static __inline__ double atof(const char *nptr) { return (strtod(nptr, NULL)); } But somehow it always results in 0.0 error_printf("found %s parsed %d…
wutzebaer
  • 14,365
  • 19
  • 99
  • 170
0
votes
4 answers

Using atof for integer

I've inherited some code (from someone who has left) and found this little snippet: double minX = xVal.find('.') == string::npos ? (double)atoi(xVal.c_str()) : atof(xVal.c_str()); double minY = yVal.find('.') == string::npos ?…
Richard
  • 2,994
  • 1
  • 19
  • 31
0
votes
1 answer

C++ convert string to double with precision

I need to convert string to double with precision up to 15 digits I have read many articles and similar questions, and they suggested to use setprecision(15) when printing out the numbers to the screen. For example: string line =…
dodgerblue
  • 255
  • 3
  • 7
  • 18
0
votes
0 answers

Using atof in NASM

I am trying to implement a simple program in NASM that takes in an unknown number of float values from the command line arguments, and adds them together. This seems not to be working for me, and i think it might be because i am using atof calls…
Drifter64
  • 1,103
  • 3
  • 11
  • 30
0
votes
1 answer

Trying to convert a string to double under conditions

I have a function which uses an std string parameter to test if there is an alpha character. It would be best to test for only numeric characters but I have not gotten that far just yet. I just am trying to get it to recognize if the input does not…
Road Rash
  • 131
  • 3
  • 14
0
votes
2 answers

Converting string to double using atof

#include .... double returnDistance(string coord1, string coord2, const vector vec) { int arr1[11], arr2[11]; istringstream iss1(coord1); int i = 0; while(iss1) { iss1 >> arr1[i]; i++; } …
soochism
  • 13
  • 1
  • 5
0
votes
3 answers

converting a string to double using atof in c

I am trying to read from a file and store it in a matrix using c. The code I have is ReadSparseInput (int nvtxs,int nsize, double vector[], int rowptr[] ,int colind [] , double values[]) { int index,row=0,column=0; int sparsity_value,…
LGG
  • 528
  • 9
  • 21
1 2 3
8 9