The decdiv() function

The decdiv() function divides two decimal type values.

Syntax

mint decdiv(n1, n2, result)   /* result = n1 / n2 */
   dec_t *n1;
   dec_t *n2;
   dec_t *result;
n1
A pointer to the decimal structure of the first operand.
n2
A pointer to the decimal structure of the second operand.
quotient
A pointer to the decimal structure that contains the quotient of n1 divided by n2.

Usage

The quotient 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.
-1202
The operation attempted to divide by zero.

Example

The file decdiv.ec in the demo directory contains the following sample program.
/*
   * decdiv.ec *

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

#include <stdio.h>

EXEC SQL include decimal;

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

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

    printf("DECDIV 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 = decdiv(&num1, &num2, &dvd))
      {
      printf("Error %d in converting divide num1 by num2\n", x);
      exit(1);
      }
    if (x = dectoasc(&dvd, result, sizeof(result), -1))
      {
      printf("Error %d in converting dividend to string\n", x);
      exit(1);
      }
    result[40] = '\0';
    printf("\t%s / %s = %s\n", string1, string2, result);

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

Output

DECDIV Sample ESQL Program running.

    480 / 80 = 6.0 

DECDIV Sample Program over.