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

atof not converting in single linked list

The input file should specify a single algebraic expression in postfix order supporting positive and negative floating point values and the following operations: Addition (+), Subtraction (-), Multiplication (*), Division (/). All floating point…
dwong
  • 57
  • 1
  • 3
  • 7
0
votes
2 answers

atof only returns integers?

I've got a routine which gets numbers in the exponent format(e.g. 2,5E-02 or 4E+06) as a QString. When I print the values I always only get integers and when they are smaller then 1 I always get 0. Does anyone know what I am doing wrong here? (with…
samoncode
  • 466
  • 2
  • 7
  • 23
0
votes
4 answers

passing table of floats as argument in c++ main

I have a c++ program, I would like the first argument of the main (argv[1]) to correspond to a table of float. Is it possible to do that?? I was thinking about putting in a string my floats separated with spaces (e.g. "1.12 3.23 4.32 1.1 ...") Is…
lizzie
  • 1,506
  • 1
  • 18
  • 31
0
votes
1 answer

c++ console application, atof

My problem is the following. I try to convert a string into double. In this way: string str = "1.1"; double d = atof(str.c_str()); But this don't work, it simply return 1; But if I try: string str = "1,1"; double d = atof(str.c_str()); it return…
Gaboros
  • 191
  • 2
  • 14
-1
votes
2 answers

Can I place the result into an int variable using either atof or atoi?

In c: Given the following code int a; char word[]= Is there a difference between? a = atoi(word) a = atof(word)
Niv
  • 850
  • 1
  • 7
  • 22
-1
votes
2 answers

atof coredump with getopt

I'm writing a C++ application which converts fahrenheit to celsius and kelvin, and kelvin to celsius and fahrenheit, etc. Since it's stupid to write an interative application here, I decided to familiarize myself with the getopt function in…
-1
votes
3 answers

C++, how to get right double value from string?

Or which type do I need to use? I have string and I try to convert it into double NFR_File.ReadString(sVal); // sVal = " 0,00003" dbl = _wtof(sVal); and get: 3.0000000000000001e-05 And I need 0,00003, because then I should write it into the file…
Nika_Rika
  • 613
  • 2
  • 6
  • 29
-1
votes
1 answer

How to convert an element of a c++ string object to a float

The goal is to parse out floating point numbers from a (string) expression and store them into a vector of floats. I am currently trying to convert the number substring to a character array with c_str() and then use the atof() function. This is…
Nick Schrock
  • 177
  • 1
  • 1
  • 11
-1
votes
4 answers

How do you check if a char is a number in c?

I'm using atof(word), where word is a char type. It works when the word is a number, such as 3 or 2, but atof doesn't distinguish when word is an operator, such as "+". Is there a better way to check whether the char is a number? I'm a newbie to CS,…
agupta2450
  • 29
  • 1
  • 1
  • 1
-1
votes
3 answers

Comparing the numeric values of ASCII strings in C

I am writing two ASCII char arrays to an LCD screen perfectly fine. However I need an if-condition to compare these two values. Originally I attempted to simply compare them like this: if(currentTemp < triggTemp) { alarmTriggered = true; } As…
James
  • 356
  • 2
  • 13
-1
votes
2 answers

could'nt read correct values of vector produced from file

I am trying to read text file line by line and then read each column as vector but when i am tryin to cout first column it shows zeros i.e. not reading the file correctly. int main(void) { ifstream myfile ("data1.txt"); string line; …
-1
votes
1 answer

Converting string to double in c++

I've made a function that receives a line and a delimiter, separates the line and returns a vector of floats (like the split function in java). This is the function: vector extractNumbers(string line, char delimiter) { vector a; …
Javi
  • 889
  • 1
  • 16
  • 41
-1
votes
2 answers

Modify String Data

I want to substring and modify my string (which is defined below). char gps[]="$GPGGA,115726.562,4512.9580,N,03033.0412,E,1,09,0,9,300,M,0,M,,*6E"; Shortly, I want to take and increase Lat,Long data which means My steps Take lat info as char or…
-1
votes
1 answer

ValueError: could not convert string to float: (buffer related?)

condensed code # attempt to condense code while preserving the parts # relevant to the question from xml.sax import handler, make_parser class pdosHandler(handler.ContentHandler): def __init__(self, data): self.data = data …
cipper
  • 1
  • 2
-1
votes
1 answer

atof creates strange result

I am trying to use atof() to convert a string to a double (obviously), but the results are not what I expect. Here is the code and debug information about the variable values BEFORE atof(): d = atof (arg); next.db = d; *debug info* arg = 0x0034f7b0…
Troncoso
  • 2,343
  • 3
  • 33
  • 52
1 2 3
8
9