The decadd() function
The decadd() function adds two decimal type values.
Syntax
mint decadd(n1, n2, sum)
dec_t *n1;
dec_t *n2;
dec_t *sum;
- n1
- A pointer to the decimal structure of the first operand.
- n2
- A pointer to the decimal structure of the second operand.
- sum
- A pointer to the decimal structure that contains the sum (n1 + n2).
Usage
The sum can be the same as either n1 or n2.
Return codes
- 0
- The operation was successful.
- -1200
- The operation resulted in overflow.
- -1201
- The operation resulted in underflow.
Example
The file decadd.ec in
the demo directory contains the following sample
program.
/*
* decadd.ec *
The following program obtains the sum of two DECIMAL numbers.
*/
#include <stdio.h>
EXEC SQL include decimal;
char string1[] = " 1000.6789"; /* leading spaces will be ignored */
char string2[] = "80";
char result[41];
main()
{
mint x;
dec_t num1, num2, sum;
printf("DECADD Sample ESQL Program running.\n\n");
if (x = deccvasc(string1, strlen(string1), &num1))
{
printf("Error %d in converting string1 to DECIMAL\n", x);
exit(1);
}
if (x = deccvasc(string2, strlen(string2), &num2))
{
printf("Error %d in converting string2 to DECIMAL\n", x);
exit(1);
}
if (x = decadd(&num1, &num2, &sum))
{
printf("Error %d in adding DECIMALs\n", x);
exit(1);
}
if (x = dectoasc(&sum, result, sizeof(result), -1))
{
printf("Error %d in converting DECIMAL result to string\n", x);
exit(1);
}
result[40] = '\0';
printf("\t%s + %s = %s\n", string1, string2, result); /* display result */
printf("\nDECADD Sample Program over.\n\n");
exit(0);
}
Output
DECADD Sample ESQL Program running.
1000.6789 + 80 = 1080.6789
DECADD Sample Program over.