The deccvdbl() function

The deccvdbl() function converts a C double type number into a decimal type number.

Syntax

mint deccvdbl(dbl_val, np)
   double dbl_val;
   dec_t *dec_val;
dbl_val
The double value that deccvdbl() converts to a decimal type value.
dec_val
A pointer to a decimal structure where deccvdbl() places the result of the conversion.

Return codes

0
The conversion was successful.
<0
The conversion failed.

Example

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

   The following program converts two double type numbers to DECIMAL numbers
   and displays the results.
*/

#include <stdio.h>

EXEC SQL include decimal;

char result[41];


main()
{
    mint x;
    dec_t num;
    double d = 2147483647;

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

    printf("Number 1 (double) = 1234.5678901234\n");
    if (x = deccvdbl((double)1234.5678901234, &num))
      {
      printf("Error %d in converting double1 to DECIMAL\n", x);
      exit(1);
      }
    if (x = dectoasc(&num, result, sizeof(result), -1))
      {
      printf("Error %d in converting DECIMAL1 to string\n", x);
      exit(1);
      }
    result[40] = '\0';
    printf("  String Value = %s\n", result);

    printf("Number 2 (double) = $.1f\n", d);
    if (x = deccvdbl(d, &num))
      {
      printf("Error %d in converting double2 to DECIMAL\n", x);
      exit(1);
      }
    if (x = dectoasc(&num, result, sizeof(result), -1))
      {
      printf("Error %d in converting DECIMAL2 to string\n", x);
      exit(1);
      }
    result[40] = '\0';
    printf("  String Value = %s\n", result);

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

Output

DECCVDBL Sample ESQL Program running.

Number 1 (double) = 1234.5678901234
  String Value = 1234.5678901234
Number 2 (double) = 2147483647.0
  String Value =  2147483647.0

DECCVDBL Sample Program over.