The deccvasc() function

The deccvasc() function converts a value held as printable characters in a C char type into a decimal type number.

Syntax

mint deccvasc(strng_val, len, dec_val)
   char *strng_val;
   mint len;
   dec_t *dec_val;
strng_val
A pointer to a string whose value deccvasc() converts to a decimal value.
len
The length of the strng_val string.
dec_val
A pointer to the decimal structure wheredeccvasc() places the result of the conversion.

Usage

The character string, strng_val, can contain the following symbols:
  • A leading sign, either a plus (+) or minus (-)
  • A decimal point, and digits to the right of the decimal point
  • An exponent that is preceded by either e or E. You can precede the exponent by a sign, either a plus (+) or minus (-).

The deccvasc() function ignores leading spaces in the character string.

Return codes

0
The conversion was successful.
-1200
The number is too large to fit into a decimal type structure (overflow).
-1201
The number is too small to fit into a decimal type structure (underflow).
-1213
The string has non-numeric characters.
-1216
The string has a bad exponent.

Example

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

   The following program converts two strings to DECIMAL numbers and displays
   the values stored in each field of the decimal structures.
*/

#include <stdio.h>

EXEC SQL include decimal;

char string1[] = "-12345.6789";
char string2[] = "480";

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

    printf("DECCVASC 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);
      }
    /*
     *  Display the exponent, sign value and number of digits in num1.
     */
    printf("\tstring1 = %s\n", string1);
    disp_dec("num1", &num1);

    /*
     *  Display the exponent, sign value and number of digits in num2.
     */
    printf("\tstring2 = %s\n", string2);
    disp_dec("num2", &num2);

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


disp_dec(s, num)
char *s;
dec_t *num;
{
    mint n;

    printf("%s dec_t structure:\n", s);
    printf("\tdec_exp = %d, dec_pos = %d, dec_ndgts = %d, dec_dgts: ",
      num->dec_exp, num->dec_pos, num->dec_ndgts);
    n = 0;
    while(n < num->dec_ndgts)
      printf("%02d ", num->dec_dgts[n++]);
    printf("\n\n");
}

Output

DECCVASC Sample ESQL Program running.

string1 =  -12345.6789
num1 dec_t structure:
        dec_exp = 3, dec_pos =  0, dec_ndgts = 5, dec_dgts: 01 23 45 67 89 

string2 =  480
num2 dec_t structure: 
       dec_exp = 2, dec_pos =  1, dec_ndgts = 2, dec_dgts: 04 80 

DECCVASC Sample Program over.