The dectrunc() function

The dectrunc() function truncates a rounded decimal type number to fractional digits.

Syntax

void dectrunc(d, s)
   dec_t *d; 
   mint s; 
d
A pointer to a decimal structure for a rounded number whose value dectrunc() truncates.
s
The number of fractional digits to which dectrunc() truncates the number. Use a positive number or zero for this argument.

Usage

The following table shows the sample output from dectrunc() with various inputs.
Value before truncation Value of s Truncated value
1.4 0 1.0
1.5 0 1.0
1.684 2 1.68
1.685 2 1.68
1.685 1 1.6
1.685 0 1.0

Example

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

   The following program truncates a DECIMAL number six times and displays 
   each result.
*/

#include <stdio.h>

EXEC SQL include decimal;

char string[] = "-12345.038572";
char result[41];

main()
{
    mint x;
    mint i = 6;          /* number of decimal places to start with */
    dec_t num1;

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

    printf("String = %s\n", string);
    while(i)
      {
      if (x = deccvasc(string, strlen(string), &num1))
         {
         printf("Error %d in converting string to DECIMAL\n", x);
         break;
         }
      dectrunc(&num1, i);
      if (x = dectoasc(&num1, result, sizeof(result), -1))
         {
         printf("Error %d in converting result to string\n", x);
         break;
         }
      result[40] = '\0';
      printf("  Truncated to %d Fractional Digits: %s\n", i--, result);
      }

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

Output

DECTRUNC Sample ESQL Program running.

String = -12345.038572
  Truncated to 6 Fractional Digits: -12345.038572                           
  Truncated to 5 Fractional Digits: -12345.03857                            
  Truncated to 4 Fractional Digits: -12345.0385                             
  Truncated to 3 Fractional Digits: -12345.038                              
  Truncated to 2 Fractional Digits: -12345.03                               
  Truncated to 1 Fractional Digits: -12345.0                                

DECTRUNC Sample Program over.