atof() Convert String to Double
#include <math.h>
#include <stdlib.h> Use either math.h or stdlib.h
double atof(string); Value of converted string
char *string; String to be converted
atof() converts 'string' to a double-precision floating-point value.
'string' is a sequence of characters that can be interpreted as a
numerical value; it has the following format:
[whitespace][sign][digits][.digits][{d|D|e|E}[sign]digits]
where:
whitespace any number of tab and space characters are ignored
sign + or -
digits one or more decimal digits
d|D|e|E exponent prefixes
If no digits appear before the decimal point, then at least one must
appear after the decimal point. (If a decimal point is present, it
must be followed by a digit.) atof() stops reading input at the
first character it cannot recognize as part of the number (likely the
null character terminating the string).
Returns: The double value of 'string'. If 'string' cannot be
converted to a double value, then 0.0 is returned. The
return value is undefined for overflow; 0.0 is returned
on underflow.
Notes: DBL_MAX_EXP (defined in <limits.h>) is the maximum
allowed exponent for double values.
-------------------------------- Example ---------------------------------
The following statements convert several strings to double values and
print the values.
#include <math.h>
#include <stdio.h>
main()
{
double x, y, z;
x = atof(" -.625d-3 (this will be -0.000625)");
y = atof("0");
z = atof(" 143248291283472348923874.2134762734E222\n");
/* prints: x=-6.250000e-004, y=0.000000e+000, z=1.432483e+245 */
printf("x=%e, y=%e, z=%e\n", x, y, z);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster