C++ strtod()

The strtod() function in C++ interprets the contents of a string as a floating point number and return its value as a double.

This function also sets a pointer to point to the first character after the last valid character of the string if there is any, otherwise the pointer is set to null.

For base 10 and the string "12abc"

Valid numeric part -> 12

First character after valid numeric part -> a

It is defined in <cstdlib> header file.


strtod() prototype

double strtod(const char* str, char** end);

The strtod() function takes string and a pointer to character as its parameter, interprets the content of string as a float number and returns a double value.


strtod() Parameters

  • str: A string having the representation of a floating point number.
  • end: Reference to an already allocated object of type char*. The value of end is set by the function to the next character in str after the last valid character. This parameter can also be a null pointer, in which case it is not used.

strtod() Return value

The strtod() function returns:

  • a double value (which is converted from the string).
  • 0.0 if no valid conversion could be performed.

If the converted value is out of the range, a range error occurs and a positive or negative HUGE_VAL is returned.


Example 1: How strtod() function works?

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char numberString[] = "12.44b 0xy";
    char *end;
    double number;

    number = strtod(numberString,&end);
    cout << "Number in String = " << numberString << endl;
    cout << "Number in Double = " << number << endl;
    cout << "End String = " << end << endl;

    return 0;
}

When you run the program, the output will be:

Number in End String = 12.44b 0xy
Number in Double = 12.44
String = b 0xy

Example 2: strtod() function without trailing characters

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char numberString[] = "12.44";
    char *end;
    double number;

    number = strtod(numberString,&end);
    cout << "Number in String = " << numberString << endl;
    cout << "Number in Double = " << number << endl;

    // If end is not Null
    if (*end) {
        cout << end;
    }
    // If end is Null
    else {
        cout << "Null pointer";
    }
    return 0;
}

When you run the program, the output will be:

Number in End String = 12.44b 0xy
Number in Double = 12.44
Null pointer

A valid floating-point value for strtod() function consists of an optional + or - sign followed by one of the following sets:

  • For decimal floating-point value:

    • A group of decimal digits (0-9), optionally containing a decimal point (.).
      For example: 13.170, -5.63, etc .

    • An optional exponent part (e or E) followed by an optional + or - sign and non-empty sequence of decimal digits.
      For example: 3.46101e+007, 13.19e-013, etc.

  • For hexadecimal floating-point value:

    • A string starting with 0x or 0X, followed by a non-empty sequence of hexadecimal digits, optionally containing a decimal point (.).
      For Example: 0xfa5, -0xb1f.24, etc.

    • An optional exponent part (p or P) followed by an optional + or - sign and non-empty sequence of hexadecimal digits.
      For Example: 0x51c.23p5, -0x2a.3p-3, etc.

  • Infinity:

    • INF or INFINITY (ignoring case).
      For Example: -Inf, InfiNiTy, etc.

  • NaN (Not a Number):

    • NAN or NANsequence (ignoring case) where sequence is a sequence of characters consisting only of alphanumeric characters or the underscore (_). The result is a quiet NaN.
      For Example: Nan, NaNab1, etc.


Example 3: How strtod() works with exponents and hexadecimals?

#include <iostream>
#include <cstring>
#include <cstdlib>

using namespace std;
int main()
{
    // initialize a exponential value
    char numberString[] = "-44.01e-3End String";
    char *end;
    double number;

    number = strtod(numberString,&end);
    cout << "Number in String = " << numberString << endl;
    cout << "Number in Double = " << number << endl;
    cout << "End String = " << end << endl << endl;

    // initialize a new hexadecimal value
    strcpy(numberString,"0xf1bc.51hello");

    number = strtod(numberString,&end);
    cout << "Number in String = " << numberString << endl;
    cout << "Number in Double = " << number << endl;
    cout << "End String = " << end << endl;
    
    return 0;
}

When you run the program, the output will be:

Number in String = -44.01e-3End String
Number in Double = -0.04401
End String = End String
Number in String = 0xf1bc.51hello
Number in Double = 61884.3
End String = hello

Example 4: strtod Cases for INFINITY and NaN

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char *end;

    cout << "INFINITY" << " to Double = " << strtod("INFINITY", &end) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "Infabc" << " to Double = " << strtod("Infabc", &end) << endl;
    cout << "End String = " << end << endl << endl;
    
    cout << "NaN12a" << " to Double = " << strtod("NaN12a", &end) << endl;
    cout << "End String = " << end << endl << endl;
    
    return 0;
}

When you run the program, the output will be:

INFINITY to Double = inf
End String =

Infabc to Double = inf
End String = abc

NaN12a to Double = nan
End String = 12a

In general, a valid floating point argument for strtod() function has the following form:

[whitespace] [- | +] [digits] [.digits] [ {e | E }[- | +]digits]

The strtod() function ignores all the leading whitespace characters until the primary non-whitespace character is found.

Then, beginning from this character, it takes as many characters as possible that forms a valid floating-point representation and converts them to a floating point value. Whatever is left of the string after the last valid character is stored in the object pointed by end.


Example 5: strtod() function with leading whitespace

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char *end;

    cout << "25.5" << " to Double = " << strtod(" 25.5", &end) << endl;
    // end pointer is set to null
    cout << "End String = " << end << endl << endl;

    // Returns 0 because of invalid conversion
    cout << "abc11.20" << " to Double = " << strtod("abc11.20", &end) << endl;
    cout << "End String = " << end << endl << endl;

    return 0;
}

When you run the program, the output will be:

25.5 to Double = 25.5
End String =

abc11.20 to Double = 0
End String = abc11.20