The dectoasc() function

The dectoasc() function converts a decimal type number to a C char type value.

Syntax

mint dectoasc(dec_val, strng_val, len, right)
   dec_t *dec_val;
   char  *strng_val;
   mint   len;
   mint   right;
dec_val
A pointer to the decimal structure whose value dectoasc() converts to a text string.
strng_val
A pointer to the first byte of the character buffer where the dectoasc() function places the text string.
len
The size of strng_val, in bytes, minus 1 for the null terminator.
right
An integer that indicates the number of decimal places to the right of the decimal point.

Usage

If right = -1, the decimal value of dec_val determines the number of decimal places.

If the decimal number does not fit into a character string of length len, dectoasc()() converts the number to an exponential notation. If the number still does not fit, dectoasc() fills the string with asterisks. If the number is shorter than the string, dectoasc() left-justifies the number and pads it on the right with blanks.

Because the character string that dectoasc()() returns is not null terminated, your program must add a null character to the string before you print it.

Return codes

0
The conversion was successful.
-1
The conversion failed.

Example

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

   The following program converts DECIMAL numbers to strings of varying sizes.
*/

#include <stdio.h>

EXEC SQL include decimal;

#define  END  sizeof(result)

char string1[] = "-12345.038782";
char string2[] = "480";
char result[40];

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

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

    printf("String Decimal Value 1 = %s\n", string1);
    if (x = deccvasc(string1, strlen(string1), &num1))
      {
      printf("Error %d in converting string1 to DECIMAL\n", x);
      exit(1);
      }
    printf("String Decimal Value 2 = %s\n", string2);
    if (x = deccvasc(string2, strlen(string2), &num2))
      {
      printf("Error %d in converting string2 to DECIMAL\n", x);
      exit(1);
      }

    printf("\nConverting Decimal back to ASCII\n");
    printf("  Executing: dectoasc(&num1, result, 5, -1)\n");
    if (x = dectoasc(&num1, result, 5, -1))
      printf("\tError %d in converting DECIMAL1 to string\n", x);
    else
      {
      result[5] = '\0';                               /* null terminate */
      printf("\tResult ='%s'\n", result);
      }

    printf("Executing: dectoasc(&num1, result, 10, -1)\n");
    if (x = dectoasc(&num1, result, 10, -1))
      printf("Error %d in converting DECIMAL1 to string\n", x);
    else
      {
      result[10] = '\0';                              /* null terminate */
      printf("\tResult = '%s'\n", result);
      }

    printf("Executing: dectoasc(&num2, result, END, 3)\n");
    if (x = dectoasc(&num2, result, END, 3))
      printf("\tError %d in converting DECIMAL2 to string\n", x);
    else
      {
      result[END-1] = '\0';                           /* null terminate */
      printf("\tResult = '%s'\n", result);
      }

    printf("\nDECTOASC Sample Program over.\n\n")
}

Output

DECTOASC Sample ESQL Program running.

String Decimal Value 1 = -12345.038782
String Decimal Value 2 = 480

Converting Decimal back to ASCII
  Executing: dectoasc(&num1, result, 5, -1)
   Error -1 in converting decimal1 to string
  Executing: dectoasc(&num1, result, 10, -1)
   Result = '-12345.039'
  Executing: dectoasc(&num2, result, END, 3)
   Result = '480.000                                '

DECTOASC Sample Program over.