strtod() Convert String to Double
#include <stdlib.h>
double strtod(nptr,endptr);
char *nptr; Pointer to string
char **endptr; Pointer to end of scan
strtod() converts a string to a double value. The string is a
sequence of characters that can be interpreted as a double value. The
string must match this sequence:
[whitespace] [sign] [digits] [.digits] [{d|D|e|E} [sign] digits]
strtod() stops reading the string at the first character that cannot
be recognized as part of a double value. If 'endptr' is not NULL, it
points to the character that stopped the scan.
Returns: The value of the floating-point number. +/-HUGE is
returned when the representation would cause an overflow
or underflow. On error, ERRNO is set to ERANGE.
-------------------------------- Example ---------------------------------
The following statements convert two strings into doubles, add them
together, and print the total.
#include <stdlib.h>
#include <stdio.h>
double total;
double dubl1, dubl2;
char *str1 = "982.50 dollars", *str2 = "540.00 dollars";
char *txt1, *txt2
main()
{
/* The space before "dollars" will stop the scanning of string. */
dubl1 = strtod(str1,&txt1);
dubl2 = strtod(str2,&txt2);
total = dubl1 + dubl2;
printf("TOTAL: %.2f ",total);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster