The dectoint() function

The dectoint() function converts a decimal type number into a C int type number.

Syntax

mint dectoint(dec_val, int_val)
   dec_t *dec_val;
   mint *int_val;
dec_val
A pointer to a decimal structure whose value dectoint() converts to a mint type value.
int_val
A pointer to a mint value where dectoint() places the result of the conversion.

Usage

The dectoint() library function converts a decimal value to a C integer. The size of a C integer depends on the hardware and operating system of the computer you are using. Therefore, the dectoint()() function equates an integer value with the SQL SMALLINT data type. The valid range of a SMALLINT is between 32767 and -32767. To convert larger decimal values to larger integers, use the dectoint() library function.

Return codes

0
The conversion was successful.
<0
The conversion failed.
-1200
The magnitude of the decimal type number is greater than 32767.

Example

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

   The following program converts two DECIMAL numbers to integers and
   displays the result of each conversion.
*/

#include <stdio.h>

EXEC SQL include decimal;

char string1 [] = "32767";
char string2 [] = "32768";


main()
{
    mint x;
    mint n = 0;
    dec_t num;

    printf("DECTOINT Sample ESQL Program running.\n\n)";

    printf("String 1 = %s\n", string1);
    if (x = deccvasc(string1,strlen(string1), &num))
      {
      printf("  Error %d in converting string1 to decimal\n", x);
      exit(1);
      }
    if (x = dectoint(&num, &n))
      printf("  Error %d in converting decimal to int\n", x);
    else
         printf("  Result = %d\n", n);

    printf("\nString 2 = %s\n", string2);
    if (x = deccvasc(string2, strlen(string2), &num))
      {
      printf("  Error %d in converting string2 to decimal\n", x);
      exit(1);
      }
    if (x = dectoint(&num, &n))
      printf("  Error %d in converting decimal to int\n", x);
    else
        printf("  Result = %d\n", n);

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

Output

DECTOINT Sample ESQL Program running.

String 1 = 32767
  Result = 32767

String 2 = 32768
  Error -1200 in converting decimal to int

DECTOINT Sample Program over.