.TH strtod() "" "" "General Function (libc)"
.PC "Convert string to floating-point number"
.B "#include <stdlib.h>"
\fBdouble strtod(\fIstring, tailptr\^\fB)\fR
\fBchar *\fIstring\^\fB; char **\fItailptr\^\fB;\fR
.PP
.II "subject sequence"
.II "string, convert to floating-point number"
.II "floating-point number, create from string"
.II "convert string to floating-point number"
.B strtod()
converts the number given in
.I string
to a double-precision floating-point number
and returns its value.
It is a more general version of the function
.BR atof() .
.B strtod()
also stores a pointer to the first character following the number
through
.IR tailptr ,
provided
.I tailptr
is not NULL.
.PP
.B strtod()
parses the input
.I string
into three portions:
beginning, subject sequence, and tail.
.PP
The
.I beginning
consists of zero or more white-space characters that begin the string.
.PP
The
.I "subject sequence"
is the portion of the input
.I string
that
.B strtod()
converts into a floating-point number.
It consists of an optional sign character,
a nonempty sequence of decimal digits optionally including
a decimal-point character, and an optional exponent.
If present, the exponent consists of
either `e' or `E' followed by
an optional sign and a nonempty sequence of decimal digits.
.B strtod()
reads characters until it encounters
either a second decimal-point character or exponent marker,
or any other non-numeral.
.PP
The
.I tail
continues from the end of the subject sequence to the null
character that ends the string.
.PP
.B strtod()
ignores the beginning portion of the string.
It converts the subject sequence to a double-precision number.
Finally, it sets the pointer pointed to by
.I tailptr
to the address of the first character of the string's tail.
.PP
.B strtod()
returns the
.B double
generated from the subject sequence.
If no subject sequence could be recognized, it returns zero
and stores the initial value of
.I string
through
.IR tailptr .
If the number represented by the subject sequence is too large
or too small to fit into a
.BR double ,
then
.B strtod()
sets the global constant
.B errno
to
.B ERANGE
and returns
.B HUGE_VAL
or zero, respectively.
If, however, the number given in the subject sequence has more digits to the
right of the decimal point than can be encoded within an IEEE
.B double
(which has a fraction of 53 bits),
.B strtod
trims the excess digits before it converts the string.
.SH Example
The following gives an example for
.BR strtod() .
.DM
#include <stdlib.h>
.DE
.DM
main()
{
	static char st[] = " 123.4 567.8";
	char *head, *tail;
.DE
.DM
	for (head = st;; head = tail) {
		double amt = strtod(head, &tail);
.DE
.DM
		 /* No token found is end of string */
		if (head == tail)
			break;
		printf("%f\en", amt);
	}
	exit(EXIT_SUCCESS);
}
.DE
.SH "See Also"
.Xr "atof()," atof
.Xr "double," double
.Xr "errno," errno
.Xr "libc," libc
.Xr "limits.h," limits.h
.Xr "stdlib.h," stdlib.h
.Xr "strtol()," strtol
.Xr "strtoul()" strtoul
.br
\*(AS, \(sc7.10.1.4
.SH Notes
.B strtod()
ignores initial white space in the string pointed to by
.IR string ;
white space is defined as being all characters so
recognized by the function
.BR isspace() .
