Questions tagged [strtod]

strtod is the C standard library function for converting an ASCII string into a floating-point number.

strtod is the C standard library function (found in the ANSI standard, section 2.13.2.4 - found in stdlib.h) which converts a string into a floating point number.


double strtod(const char *ptr, char **endptr)

  • ptr: A pointer to the string that will be converted
  • endptr: A pointer to where the last successful character match occurred.

    For example, calling strtod on the string "123abc" yields an endptr which is equal to ptr + 3. This is often used to test whether or not a string conversion went correctly - if no conversion took place, then *endptr == ptr.

    Note that this does not occur if endptr is NULL.


According to ANSI,

The number may consist of an optional sign, a string of digits with an optional decimal character, and an optional e or E followed by a optionally signed exponent.

(This effectively allows both "normal" decimal numbers, as well as scientific notation).

When the converter experiences either an overflow or underflow, errno is set to ERANGE, and either 0 (for underflows) or HUGE_VAL (for overflows) - it is thus recommended to clear errno before calling this function, otherwise an underflow error and the conversion from "0" are impossible to differentiate.

61 questions
0
votes
0 answers

strtod using classic format?

Now functions strtod and strtol support parsing hex number format, infinity, NaN and other non-standard formats. But how to convert string to number using only classic format: 123, 1.23, 0.123, -123, 1.23e10, 1.23e-10 ? Not: +123, .123, 0xabcd,…
iOS User
  • 101
  • 1
  • 8
0
votes
1 answer

Using strtof in a loop to parse numbers from a char buffer

I have a question about allocating and releasing memory. I want to read a char buffer in a loop and save the float values ​​to a vector. I get the buffer by reading an fstream. But my approach always crashes when deleting the buffers at the end. Is…
Mario
  • 214
  • 2
  • 10
0
votes
1 answer

Strange things happen after using std::strtod

Here's a small lambda function I use as an helper function in the code for my lexical analyzer: auto buildnumber = [&line, &i] () -> Token* { Token* new_number = new Token(nullptr, number, line); char** after_number = nullptr; double*…
user6245072
  • 2,051
  • 21
  • 34
0
votes
3 answers

fetching string and converting to double

I'm trying to create a function which can determine if an input can be converted perfectly into a double and then be able to store it into an array of doubles. For example, an input of "12.3a" is invalid. From what I know, strtod can still convert…
0
votes
2 answers

C - extracting all Numbers from string

I got a String like by reading a file which got online one line. The C is a separator between the numbers. 3000C9.5452C5.644 ... Now I want to extract all those numbers and write them into an double array whichs is called Matrix. …
Sven Bamberger
  • 899
  • 2
  • 10
  • 21
0
votes
1 answer

convert char * to double,without losing precision in c

I need some help. I have to convert and STORE a char * into a double or long double without losing precision. Indeed, i tried to use strtold and atof methods (also strtold), but these methods are rounding the value. For example: char * key …
venilla
  • 61
  • 1
  • 9
0
votes
2 answers

read from csv, strtod can't read string with numbers

when I read the follow file "X","Pa(X)","Time","pa","xixj","Value" "X",0,5,0,"0-0","123.814935276" " "," "," "," ","0-1","234" " "," "," "," ","0-2","100" " "," "," "," ","1-0","166" " "," "," "," ","1-1","203.0866414" " "," "," ","…
adry_b89
  • 43
  • 9
0
votes
2 answers

Why does strol need a radix when strtod does not?

I've been working with cin input recently, and I discovered that strtol needs a radix: long int strtol (const char* str, char** endptr, int base);, but strtod does not: double strtod (const char* str, char** endptr);. Obviously, double numbers can…
0
votes
1 answer

C - strtod() does not recognise the decimal dot

I have a buffer in which there are "random" numbers, separated by a coma. For example : "1589.3,12478.359,485.39971" etc I need to check the length of the decimal part and the integer part. I made some research, and I decided to use the strtod() on…
Psycho
  • 1
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
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
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
1 answer

Why `strtod` just ignore digits when current is already exceed `DBL_MAX*0.1`

The source code (I am not sure which version is this, it is just an excerpt from the website). At the very beginning of the for loop the comment says that "We've gotten enough digits, and we are just gonna ignore the rest". Why is this true? And why…
Joey.Z
  • 4,492
  • 4
  • 38
  • 63
-1
votes
2 answers

Input validation for Integer and Double data types

Because there was an error in the code when I posted this question, it is not a good question. I have deleted and replaced it with a link to a correct solution. Correct Solution for Input Validation
user898058
-1
votes
1 answer

C - strtod seems to add Digits

I got a Problem with my strtod() it seems to add some digits. I'm reading 2\t5241021356985.0302\t9.09\t825.45 from a File And after skipping the integer 2 I get the following output output: 5241021356985.030273 9 .090000 Here is my Code char…
Sven Bamberger
  • 899
  • 2
  • 10
  • 21