Contents


Decimal Functions                 

Use these functions to manipulate decimal data type values.  Using other functions may produce unpredictable results.


decadd()

Purpose:

To add two decimal values

Syntax:

mint decadd(dec_t *n1, struct decimal *n2, struct decimal *n3);

Notes:

  1. n1 is a pointer to the decimal structure of the first operand.
  2. n2 is a pointer to the decimal structure of the second operand.
  3. n3 is a pointer to the decimal structure that contains the sum (n1 + n2).

Return code:

0         The operation was successful.
-1200  The operation resulted in overflow.
-1201  The operation resulted in underflow.


decsub()

Purpose:

To subtract two decimal values

Syntax:

mint decsub(dec_t *n1, struct decimal *n2, struct decimal *n3);

Notes:

  1. n1 is a pointer to the decimal structure of the first operand.
  2. n2 is a pointer to the decimal structure of the number to be subtracted.
  3. n3 is a pointer to the decimal structure that contains the result of (n1 minus n2).

Return code:

0         The operation was successful.
-1200  The operation resulted in overflow.
-1201  The operation resulted in underflow.


decmul()

Purpose:

To multiply two decimal values

Syntax:

int decmul(dec_t *n1, struct decimal *n2, struct decimal *n3);

Notes:

  1. n1 is a pointer to the decimal structure of the first operand.
  2. n2 is a pointer to the decimal structure of the second operand.
  3. n3 is a pointer to the decimal structure that contains the product of (n1 times n2).

Return code:

0         The operation was successful.
-1200  The operation resulted in overflow.
-1201  The operation resulted in underflow.

 


decdiv()

Purpose:

To divide one decimal value by another(n1 divided by n2)

Syntax:

mint decdiv(dec_t *n1, struct decimal *n2, struct decimal *n3);

Notes:

  1. n1 is a pointer to the decimal structure of the number to be divided.
  2. n2 is a pointer to the decimal structure of the number that is the divisor.
  3. n3 is a pointer to the decimal structure that contains the quotient of (n1 divided by n2).

Return code:

0         The operation was successful.
-1200  The operation resulted in overflow.
-1201  The operation resulted in underflow.
-1202  The operation attempted to divide by zero.


deccmp()

Purpose:

To compare two decimal values

Syntax:

mint deccmp(dec_t *n1, struct decimal *n2);

Notes:

  1. n1 is a pointer to the decimal structure of the first number to compare.
  2. n2 is a pointer to the decimal structure of the second number to compare.

Return code:

-1                          The first value is less than the second value.
0                           The two values are identical.
1                           The first value is greater than the second value.
DECUNKNOWN Either value is null.


deccopy()

Purpose:

To copy one decimal value to another

Syntax:

void deccopy(dec_t *n1, struct decimal *n2);

Notes:

  1. n1 is a pointer to the value held in the source decimal structure.
  2. n2 is a pointer to the target decimal structure.

deccvasc()

Purpose:

To convert a string value to a decimal

Syntax:

mint deccvasc(char *cp, mint len, dec_t *np);

Notes:

  1. cp is a pointer to a string to be converted to a decimal value.
  2. len is the length of the string.
  3. np is a pointer to the decimal structure which contains the result of the conversion.

Return code:

0         The conversion was successful.
-1200  The number is too large to fit into a decimal type structure (overflow).


deccvdbl()

Purpose:

To convert a double value to a decimal

Syntax:

mint deccvdbl(double dbl, dec_t *np);

Notes:

  1. dbl is the double value to convert to a decimal type value.
  2. np is a pointer to a decimal structure containing the result of the conversion.

Return code:

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


deccvint()

Purpose:

To convert an integer value to a decimal

Syntax:

mint deccvint(mint in, dec_t *np);

Notes:

  1. in is the mint value to convert to a decimal type value.
  2. np is a pointer to a decimal structure to contain the result of the conversion.

Return code:

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


deccvlong()

Purpose:

To convert a long integer value to a decimal

Syntax:

mint deccvlong(int4 lng, dec_t *np);

Notes:

  1. lng is the int4 value to convert to a decimal type value.
    np is a pointer to a decimal structure to contain the result of the conversion.

Return code:

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


dececvt()

Purpose:

To convert a decimal value to a string value, specifying the length of the string (the total number of digits)

Syntax:

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

Notes:

  1. np is a pointer to a decimal structure that contains the decimal value you want to convert.
  2. ndigit is the length of the ASCII string.
  3. decpt is 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.
  4. sign is a pointer to the sign of the result. If the sign of the result is negative, sign is nonzero; otherwise, sign is zero.

decfcvt()

Purpose:

To convert a decimal value to a string value, specifying the number of digits to the right of the decimal point

Syntax:

char *decfcvt(dec_t *np, mint ndigit, mint *decpt, mint *sign);

Notes:

  1. np is a pointer to a decimal structure that contains the decimal value you want to convert.
  2. ndigit is the number of digits to the right of the decimal point.
  3. decpt is 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.
  4. sign is a pointer to the sign of the result. If the sign of the result is negative, sign is nonzero; otherwise, sign is zero.

decround()

Purpose:

To round a decimal value, specifying the number of digits to the right of the decimal point

Syntax:

void decround(dec_t *np, mint dec_round);

Notes:

  1. np is a pointer to a decimal structure whose value is to be rounded. Use a positive number for the np argument.
  2. dec_round is the number of fractional digits to which the value is rounded.

dectoasc()

Purpose:

To convert a decimal value to an ASCII string, specifying the length of the string and the number of digits to the right of the decimal point

Syntax:

mint dectoasc(dec_t *np, char *cp, mint len, mint right);

Notes:

  1. np is a pointer to the decimal structure to convert to a text string.
  2. cp is a pointer to the first byte of the character buffer to hold the text string.
  3. len is the size of strng_val, in bytes, minus 1 for the null terminator.
  4. right is an integer that indicates the number of decimal places to the right of the decimal point.
  5. 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 code:

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


dectodbl()

Purpose:

To convert a decimal value to a double value

Syntax:

mint dectodbl(dec_t *np, double *dblp);

Notes:

  1. np is a pointer to the decimal structure to convert to a double type value.
  2. dblp is a pointer to a double type where dectodbl() places the result of the conversion.

Return code:

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


dectoint()

Purpose:

To convert a decimal value to an integer

Syntax:

mint dectoint(dec_t *np, mint *ip);

Notes:

  1. np  is a pointer to the decimal structure to convert to a mint type value.
  2. ip is a pointer to a mint value where dectoint() places the result of the conversion.

Return code:

0         The conversion was successful.
<0       The conversion failed.
-1200 The magnitude of the decimal type number is greater than 32767.


dectolong()

Purpose:

To convert a decimal value to a long integer

Syntax:

mint dectolong(dec_t *np, int4 *lngp);

Notes:

  1. np  is a pointer to the decimal structure to convert to an int4 integer.
  2. lngp is a pointer to an int4 integer where dectolong() places the result of the conversion.

Return code:

0         The conversion was successful.
-1200 The magnitude of the decimal type number is greater than 2,147,483,647.


dectrunc()

Purpose:

To truncate a decimal value, specifying the number of digits to the right of the decimal point

Syntax:

void dectrunc(dec_t *np, mint trunc);

Notes:

  1. np is a pointer to the decimal structure for a rounded number to truncate.
  2. trunc is the number of fractional digits to which dectrunc() truncates the number. Use a positive number or zero for this argument.

deccvflt()

Purpose:

To convert a float to a decimal value

Syntax:

mint deccvflt(float source, dec_t *destination);

Notes:

  1. source is the float value to be converted.
  2. destination is a pointer to the structure where the decimal value is placed.

Return code:

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


dectoflt()

Purpose:

To convert a decimal value to a float

Syntax:

mint dectoflt(dec_t *source, float *destination);

Notes:

  1. source is a pointer to the decimal value to convert.
  2. destination is a pointer to the resulting float value.

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


rfmtdec()

Purpose:

To convert a decimal value to a string having a specified format

Syntax:

int rfmtdec(dec_t *dec, char *format, char *outbuf);

Notes:

  1. dec is a pointer to the decimal value to format.
  2. format is a pointer to a character buffer that contains the formatting mask to use.
  3. outbuf is a pointer to a character buffer that receives the resulting formatted string.

Return code:

0         The conversion was successful.
-1211 The program ran out of memory (memory-allocation error).
-1217 The format string is too large.

Formatting Mask:

Formatting Mask

A numeric-formatting mask specifies a format to apply to some numeric value. This mask is a combination of the following formatting characters:

Character  Description
*    This character fills with asterisks any positions in the display field that would otherwise be blank.
&    This character fills with zeros any positions in the display field that would otherwise be blank.
#    This character changes leading zeros to blanks. Use this character to specify the maximum leftward extent of a field.
<    This character left-justifies the numbers in the display field. It changes leading zeros to a null string.


 
 This character indicates the symbol that separates groups of three digits (counting leftward from the units position) in the whole-number part of the value. By default, this symbol is a comma. You can set the symbol with the DBMONEY environment variable. In a formatted number, this symbol appears only if the whole-number part of the value has four or more digits.
.  

 
 This character indicates the symbol that separates the whole-number part of a money value from the fractional part. By default, this symbol is a period. You can set the symbol with the DBMONEY environment variable. You can have only one period in a format string.
-  

 
 This character is a literal. It appears as a minus sign when the expression  is less than zero. When you group several minus signs in a row, a single minus sign floats to the rightmost position that it can occupy; it does not interfere with the number and its currency symbol.


 
 This character is a literal. It appears as a plus sign when the expression is greater than or equal to zero and as a minus sign when expr1 is less than zero. When you group several plus signs in a row, a single plus or minus sign floats to the rightmost position that it can occupy; it does not interfere with the number and its currency symbol.


 
 This character is a literal. It appears as a left parenthesis to the left of a negative number. It is one of the pair of accounting parentheses that replace a minus sign for a negative number. When you group several in a row, a single left parenthesis floats to the rightmost position that it can occupy; it does not interfere with the number and its currency symbol.
)    This is one of the pair of accounting parentheses that replace a minus sign for a negative value.
$  


 
 This character displays the currency symbol that appears at the front of the numeric value. By default, the currency symbol is the dollar sign ($). You can set the currency symbol with the DBMONEY environment variable. When you group several dollar signs in a row, a single currency symbol floats to the rightmost position that it can occupy; it does not interfere with the number.

Any other characters in the formatting mask are reproduced literally in the result.

Examples:

Mask  Numeric value  Formatted String
-##,###.##

  
 -12345.67
   12345.67
       113.11
-12,234.67
b12,345.67
bbbb113.11
##,###.##
 
 -12345.67
   12345.67
12,345.67
12,345.67
--,---.&&  -445.67  bb-445.67
$$,$$$.&&
 
2345.67
445.67
$2,345.67
bb$445.67