The dececvt() and decfcvt() functions

The dececvt() and decfcvt() functions are analogous to the subroutines under ECVT(3) in section three of the UNIX™ Programmer's Manual. The dececvt() function works in the same fashion as the ecvt(3) function, and the decfcvt() function works in the same fashion as the fcvt(3) function. They both convert a decimal type number to a C char type value.

Syntax

char *dececvt(dec_val, ndigit, decpt, sign)
   dec_t *dec_val;
   mint ndigit;
   mint *decpt;
   mint *sign;

char *decfcvt(dec_val, ndigit, decpt, sign)
   dec_t *dec_val;
   mint ndigit;
   mint *decpt;
   mint *sign;
dec_val
A pointer to a decimal structure that contains the decimal value you want these functions to convert.
ndigit
The length of the ASCII string for dececvt(). It is the number of digits to the right of the decimal point for decfcvt().
decpt
A pointer to an integer that is the position of the decimal point relative to the start of the string. A negative or zero value for *decpt means to the left of the returned digits.
sign
A pointer to the sign of the result. If the sign of the result is negative, *sign is nonzero; otherwise, *sign is zero.

Usage

The dececvt() function converts the decimal value to which np points into a null-terminated string of ndigit ASCII digits and returns a pointer to the string. A subsequent call to this function overwrites the string.

The dececvt() function rounds low-order digits.

The decfcvt() function is identical to dececvt(), except that ndigit specifies the number of digits to the right of the decimal point instead of the total number of digits.

Let dec_val point to a decimal value of 12345.67 and suppress all arguments except ndigit. The following table shows the values that the dececvt() function returns for four different ndigit values.
ndigit value Return string *decpt value *sign
4 "1235" 5 0
10 "1234567000" 5 0
1 "1" 5 0
3 "123" 5 0
For more examples of dec_val and ndigit values, see the sample output of the dececvt.ec demonstration program on Example of dececvt().
Important: When you write thread-safe applications, do not use the dececvt() or decfcvt() library functions. Instead, use their thread-safe equivalents, The ifx_dececvt() and ifx_decfcvt() function For more information, see HCL OneDB libraries

Example of dececvt()

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

    The following program converts a series of DECIMAL numbers to fixed
    strings of 20 ASCII digits.  For each conversion it displays the resulting
    string, the decimal position from the beginning of the string and the
    sign value. 
*/

#include <stdio.h>

EXEC SQL include decimal;


char *strings[] =
   {
   "210203.204",
   "4894",
   "443.334899312",
   "-12344455",
   "12345.67",
   ".001234",
   0
   };

char result[40];

main()
{
    mint x;
    mint i = 0, f, sign;
    dec_t num;
    char *dp, *dececvt();

    printf("DECECVT Sample ESQL Program running.\n\n");
   while(strings[i])
      {
      if (x = deccvasc(strings[i], strlen(strings[i]), &num))
         {
         printf("Error %d in converting string [%s] to DECIMAL\n",
            x, strings[i]);
         break;
         }
      printf("\Input string[%d]: %s\n", i, strings[i]);

      dp = dececvt(&num, 20, &f, &sign);    /* to 20-char ASCII string */
      printf(" Output of dececvt(&num, 20, ...): %c%s  decpt: %d  sign: %d\n",
         (sign ? '-' : '+'), dp, f, sign);

      dp = dececvt(&num, 10, &f, &sign);    /* to 10-char ASCII string */
      /* display result */
      printf(" Output of dececvt(&num, 10, ...): %c%s  decpt: %d  sign: %d\n",
         (sign ? '-' : '+'), dp, f, sign);

      dp = dececvt(&num, 4, &f, &sign);    /* to 4-char ASCII string */
      /* display result */
      printf(" Output of dececvt(&num, 4, ...): %c%s  decpt: %d  sign: %d\n",
         (sign ? '-' : '+'), dp, f, sign);

      dp = dececvt(&num, 3, &f, &sign);    /* to 3-char ASCII string */
      /* display result */
      printf(" Output of dececvt(&num, 3, ...): %c%s  decpt: %d  sign: %d\n",
         (sign ? '-' : '+'), dp, f, sign);
      dp = dececvt(&num, 1, &f, &sign);    /* to 1-char ASCII string */
      /* display result */
      printf(" Output of dececvt(&num, 1, ...): %c%s  decpt: %d  sign: %d\n",
         (sign ? '-' : '+'), dp, f, sign);

      ++i;                                            /* next string */
      }

   printf("\nDECECVT Sample Program over.\n\n");
}

Output of dececvt()

DECECVT Sample ESQL Program running.


Input string[0]: 210203.204
 Output of dececvt: +2102  decpt: 6  sign: 0
 Output of dececvt: +2102032040  decpt: 6  sign: 0
 Output of dececvt: +2  decpt: 6  sign: 0
 Output of dececvt: +210  decpt: 6  sign: 0

Input string[1]: 4894
 Output of dececvt: +4894  decpt: 4  sign: 0
 Output of dececvt: +4894000000  decpt: 4  sign: 0
 Output of dececvt: +5  decpt: 4  sign: 0
 Output of dececvt: +489  decpt: 4  sign: 0

Input string[2]: 443.334899312
 Output of dececvt: +4433  decpt: 3  sign: 0
 Output of dececvt: +4433348993  decpt: 3  sign: 0
 Output of dececvt: +4  decpt: 3  sign: 0
 Output of dececvt: +443  decpt: 3  sign: 0

Input string[3]: -12344455
 Output of dececvt: -1234  decpt: 8  sign: 1
 Output of dececvt: -1234445500  decpt: 8  sign: 1
 Output of dececvt: -1  decpt: 8  sign: 1
 Output of dececvt: -123  decpt: 8  sign: 1

Input string[4]: 12345.67
 Output of dececvt: +1235  decpt: 5  sign: 0
 Output of dececvt: +1234567000  decpt: 5  sign: 0
 Output of dececvt: +1  decpt: 5  sign: 0
 Output of dececvt: +123  decpt: 5  sign: 0

Input string[5]: .001234
 Output of dececvt: +1234  decpt: -2  sign: 0
 Output of dececvt: +1234000000  decpt: -2  sign: 0
 Output of dececvt: +1  decpt: -2  sign: 0
 Output of dececvt: +123  decpt: -2  sign: 0

DECECVT Sample Program over.

Example of decfcvt()

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

    The following program converts a series of DECIMAL numbers to strings 
    of ASCII digits with 3 digits to the right of the decimal point.  For 
    each conversion it displays the resulting string, the position of the 
    decimal point from the beginning of the string and the sign value. 
*/

#include <stdio.h>

EXEC SQL include decimal;

char *strings[] =
   {
   "210203.204",
   "4894",
   "443.334899312",
   "-12344455",
   0
   };


main()
{
    mint x;
    dec_t num;
    mint i = 0, f, sign;
    char *dp, *decfcvt();


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

    while(strings[i])
      {
      if (x = deccvasc(strings[i], strlen(strings[i]), &num))
         {
         printf("Error %d in converting string [%s] to DECIMAL\n",
            x, strings[i]);
         break;
         }

      dp = decfcvt(&num, 3, &f, &sign);               /* to ASCII string */

      /* display result */
      printf("Input string[%d]: %s\n", i, strings[i]);
      printf("  Output of decfcvt: %c%*.*s.%s  decpt: %d  sign: %d\n\n",
         (sign ? '-' : '+'), f, f, dp, dp+f, f, sign);
      ++i;                                            /* next string */
      }

    printf("\nDECFCVT Sample Program over.\n\n");
}

Output of decfcvt()

DECFCVT Sample ESQL Program running.

Input  string[0]: 210203.204
  Output of decfcvt: +210203.204  decpt: 6  sign: 0

Input  string[1]: 4894
  Output of decfcvt: +4894.000  decpt: 4  sign: 0

Input  string[2]: 443.334899312
  Output of decfcvt: +443.335  decpt: 3  sign: 0

Input  string[3]: -12344455
  Output of decfcvt: -12344455.000  decpt: 8  sign: 1

DECFCVT Sample Program over.