The dectodbl() function
The dectodbl() function converts a decimal type number into a C double type number.
Syntax
mint dectodbl(dec_val, dbl_val)
dec_t *dec_val;
double *dbl_val;
- dec_val
- A pointer to a decimal structure whose value dectodbl() converts to a double type value.
- dbl_val
- A pointer to a double type where dectodbl() places the result of the conversion.
Usage
The floating-point format of the host computer can result in loss of precision in the conversion of a decimal type number to a double type number.
Return codes
- 0
- The conversion was successful.
- <0
- The conversion failed.
Example
The dectodbl.ec file
in the demo directory contains the following
sample program.
/*
* dectodbl.ec *
The following program converts two DECIMAL numbers to doubles and displays
the results.
*/
#include <stdio.h>
EXEC SQL include decimal;
char string1[] = "2949.3829398204382";
char string2[] = "3238299493";
char result[40];
main()
{
mint x;
double d = 0;
dec_t num;
printf("DECTODBL Sample ESQL Program running.\n\n");
if (x = deccvasc(string1, strlen(string1), &num))
{
printf("Error %d in converting string1 to DECIMAL\n", x);
exit(1);
}
if (x = dectodbl(&num, &d))
{
printf("Error %d in converting DECIMAL1 to double\n", x);
exit(1);
}
printf("String 1 = %s\n", string1);
printf("Double value = %.15f\n", d);
if (x = deccvasc(string2, strlen(string2), &num))
{
printf("Error %d in converting string2 to DECIMAL\n", x);
exit(1);
}
if (x = dectodbl(&num, &d))
{
printf("Error %d in converting DECIMAL2 to double\n", x);
exit(1);
}
printf("String 2 = %s\n", string2);
printf("Double value = %f\n", d);
printf("\nDECTODBL Sample Program over.\n\n");
exit(0);
}
Output
DECTODBL Sample ESQL Program running.
String 1 = 2949.3829398204382
Double value = 2949.382939820438423
String 2 = 3238299493
Double value = 3238299493.000000
DECTODBL Sample Program over.