The decround() function
The decround() function rounds a decimal type number to fractional digits.
Syntax
void decround(d, s)
dec_t *d;
mint s;
- d
- A pointer to a decimal structure whose value the decround() function rounds.
- s
- The number of fractional digits to which decround() rounds d. Use a positive number for the s argument.
Usage
The rounding factor is 5x10-s-1.
To round a value, the decround() function adds
the rounding factor to a positive number or subtracts this factor
from a negative number. It then truncates to s digits,
as the following table shows.
Value before round | Value of s | Rounded value |
---|---|---|
1.4 | 0 | 1.0 |
1.5 | 0 | 2.0 |
1.684 | 2 | 1.68 |
1.685 | 2 | 1.69 |
1.685 | 1 | 1.7 |
1.685 | 0 | 2.0 |
Return codes
The file decround.ec in
the demo directory contains the following sample
program.
/*
* decround.ec *
The following program rounds a DECIMAL type number six times and displays
the result of each operation.
*/
#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("DECROUND 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;
}
decround(&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(" Rounded to %d Fractional Digits: %s\n", i--, result);
}
printf("\nDECROUND Sample Program over.\n\n");
}
Output
DECROUND Sample ESQL Program running.
String = -12345.038572
Rounded to 6 Fractional Digits: -12345.038572
Rounded to 5 Fractional Digits: -12345.03857
Rounded to 4 Fractional Digits: -12345.0386
Rounded to 3 Fractional Digits: -12345.039
Rounded to 2 Fractional Digits: -12345.04
Rounded to 1 Fractional Digits: -12345.
DECROUND Sample Program over.