The decsub() function

The decsub() function subtracts two decimal type values.

Syntax

mint decsub(n1, n2, difference)
   dec_t *n1;
   dec_t *n2;
   dec_t *difference;
n1
A pointer to the decimal structure of the first operand.
n2
A pointer to the decimal structure of the second operand.
difference
A pointer to the decimal structure that contains the difference of n1 minus n2.

Usage

The difference 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 decsub.ec in the demo directory contains the following sample program.
/*
   * decsub.ec *

   The following program subtracts two DECIMAL numbers and displays the result.
*/

#include <stdio.h>

EXEC SQL include decimal;

char string1[] = "1000.038782";
char string2[] = "480";
char result[41];

main()
{
    mint x;
    dec_t num1, num2, diff;

    printf("DECSUB 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 = decsub(&num1, &num2, &diff))
      {
      printf("Error %d in subtracting decimals\n", x);
      exit(1);
      }
   if (x = dectoasc(&diff, result, sizeof(result), -1))
      {
      printf("Error %d in converting result to string\n", x);
      exit(1);
      }
    result[40] = '\0';
    printf("\t%s - %s = %s\n", string1, string2, result);

    printf("\nDECSUB Sample Program over.\n\n");
    exit(0);
}

Output

DECSUB Sample ESQL Program running.

   1000.038782 - 480 = 520.038782

DECSUB Sample Program over.