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

How to convert text into a float number in C

I am writing a code that would parse incoming UART messages and extract the information that I want. I will be sending a command and then a floating number for example: "SET_VOLTAGE:2.4" I would like to know how can I parse the "2.4" in to an actual…
Lukas
  • 29
  • 8
0
votes
1 answer

how to convert a numeric into a char number?

The problem is the next, generate a new string, 1. In the firt place : the firts letter of the name 2. In the second place : the third letter of the name 3. In the thitd place : The last letter of the name 4. In the fourth place : the lenght of the…
0
votes
1 answer

How to use substr with atof?

Reading from a .txt file, I would like to convert some values from a file, converting a string into doubles. Normally, I can print the desired values: string line; ifstream f; f.open("set11.txt"); if(f.is_open()) { for(int i = 0; i < 3; i++)…
0
votes
0 answers

C error using the function atof, changing a string to a double

I´m learning to use these functions that change a string to an int or double, but atof doesn´t work for me. char cadena[20]="3.1416"; double x=atof(cadena); printf("%g",x); I have this simple code, but the output of x is 7.73471e+006. Why? Atoi…
IssacCloudVII
  • 41
  • 1
  • 5
0
votes
1 answer

Why atof function does not work properly?

I am coding in C in a university course and we got a project to take equations from the user and give solutions for matrices etc... My problem is that I am trying to use atof() function and for a reason I can't find in the same loop once it works…
0
votes
1 answer

Converting Strings from a .csv file to doubles

Having trouble with the converting of strings to doubles. I've tried using strtod, but that does the same thing. It seems like this should work just find but perhaps using strtok has something to do with it. data[i].calories is a double of course.…
0
votes
3 answers

Create a precise atof() implementation in c

I have written an atof() implementation in c . I am facing rounding off errors in this implementation . So , putting in a test value of 1236.965 gives a result of 1236.964966 but the library atof() function reurns 1236.965000 . My question is , how…
John
  • 141
  • 1
  • 7
0
votes
1 answer

Converting char[] to float or double - c++

UPDATE according to TNTFreaks instructions answer. I have a char variable data defined such as follow: #define CMD_LEN 4 char data[CMD_LEN + 1]; float n1; # I pass the data variable to `serial_read` function n = serial_read(serial_fd, data,…
bgarcial
  • 2,915
  • 10
  • 56
  • 123
0
votes
0 answers

Practical way to parse a float with newlib without locale support

I'm experimenting with NIOS II soft core, trying to minimize the footprint of my embedded app. One of the biggest gains I get comes from using the small C library (p 206): The full newlib library functionality is often unnecessary for embedded…
Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53
0
votes
1 answer

atof() in K&R book 4.2

I'm exercising atof() which is provided in K&R book 4.2. However, when I compile the following code, I get 1952.690000 instead of 1237.88 I tried, but still don't know where's wrong. Maybe there is a problem with type conversions? #include…
1MinLeft
  • 93
  • 7
0
votes
3 answers

Converting a string like "2.12e-6" to a double

Is there a built in function in c++ that can handle converting a string like "2.12e-6" to a double?
Ian Burris
  • 6,325
  • 21
  • 59
  • 80
0
votes
1 answer

Atof is not working in C and without atof is also not working in debug

I have problems with Atof function. I am trying to convert string to float but it is not giving any error when I try in Coocox software in Debug section, Output is not showing anything. I tried two functions Atoi and Atof. When I use Atoi there is…
Nobody
  • 79
  • 13
0
votes
1 answer

atof() returning float instead of double

I have text files containing numbers with precision upto 12 decimal places. So to load the number into a c++ vec I wrote this void txt2vec(const std::string& file, std::vector& data) { std::string line; std::ifstream myfile(file); if…
0
votes
3 answers

Expected unqualified-id while using strtok

I am trying to split a string with strtok(). I am getting floating numbers as a string and then by using atof(), i am converting them into floating point and then put them into another array. At least i am trying. My code is like: int main() { float…
NoWay
  • 11
  • 1
  • 5
0
votes
1 answer

C++ atof/_wtof, rounding error

I have a CString variable which I need to convert into double CString sVal(_T(" 4.2")); double dbl2 = _wtof(sVal); And I got dbl2 = 4.0000 instead of 4.2. What could be the reason for the rounding?
Nika_Rika
  • 613
  • 2
  • 6
  • 29
1 2 3
8 9